Jump to content

BBCode


RobertP

Recommended Posts

i have 2 very simple concepts, and 1 very annoying problem that i think i have solved. I am posting here to be safe.

 

My Original Concept

$string = 'welcome [u]to[/u] [b]php freaks[/b]!';
$expressions = array(
    '#([[]b[]])(.*)([[]/b[]])#e',
    '#([[]u[]])(.*)([[]/u[]])#e'
);
$results = array(
    '(\'<span style="font-weight:bold;">$2</span>\')',
    '(\'<span style="text-decoration:underline;">$2</span>\')'
);
echo preg_replace($expressions,$results,$string);

 

 

My New Concept

$string = 'welcome [u]to[/u] [b]php freaks[/b]!';
$expressions = array(
    '/\[b\](.*?)\[\/b\]/',
    '/\[u\](.*?)\[\/u\]/'
);
$results = array(
    '<span style=font-weight:bold;>\\1</span>',
    '<span style=text-decoration:underline;>\\1</span>'
);
echo preg_replace($expressions,$results,$string);

 

1st issue is when i have multiple codes, for example

[b]hi[/b], whats [b]your[/b] name

bold results are

[/b]hi, whats [b]your

 

the 1st issue is solved with my new concept, however now i have an issue, my code is adding quotes for me to my html. i am not sure how to explain it.

for example

<span style=text-dexoration:underline;>blaa</span>

gets rendered like

<span style="text-dexoration:underline;">blaa</span>

 

i will look into this, i am not sure whats causing it atm :S

 

 

 

i need some feed back on my 2 concepts please :)

Link to comment
Share on other sites

How are you viewing the source? If it's through a tool like Firebug or Chrome's developer tools, then it probably re-formats the HTML (correctly). Why are you leaving out the quotes out of interest?

 

i am using chrome, and if i add the quotes like this

<span style="font-weight:bold;">blaa</span>

 

i receive this as a result

<span style="\"font-weight:bold;"\">blaa</span>

 

adds a slash, and a new quote :S

Link to comment
Share on other sites

Are you saving this to a database? Or formatting it on output?

 

If you're saving it to a database and are using mysql_real_escape_string then you'll need to strip them off for output with stripslashes().

 

If you're not saving it to a database you probably have magic quotes on, so turn that off in the php.ini.

Link to comment
Share on other sites

1) I wouldn't replace the BBCode before you save the data. What you have in the database should be kept as close to what the user entered as possible. What will happen when you allow the user to edit their post/input? Or what if you decide to serve the data on another platform (Android / iOS)? You should parse the BBCode and convert it to HTML as it's output to a web page.

 

2) On top of what scootstah suggested, can you show us the code you're using to insert / display the data?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.