2

I would like to make a [code][/code] tag for bbcode so that what would be inside wouldn't be taken into account by the php regex that I made.

Example :

Hello [b]newbie[/b], to write in bold, use the following : [code][b](YOURTEXT)[/b][/code]

Should return in HTML :

Hello <strong>newbie</strong>, to write in bold, use the following : [b](YOURTEXT)[/b]

Here is a view of a part of my bbcode function :

<?
function bbcode($var) {
   $var = preg_replace('`\[b\](.+)\[/b\]`isU', '<strong>$1</strong>', $var); 
   $var = preg_replace('`\[i\](.+)\[/i\]`isU', '<em>$1</em>', $var);
   $var = preg_replace('`\[u\](.+)\[/u\]`isU', '<u>$1</u>', $var);
   return $var;
}
?>

Thank you in advance for your kind help !


EDIT : Here is how I finally made it work :

<? 
function bbcode($var) {
$var2 = preg_split('`(\[code].*?\[/code])`isU', $var, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);

$var = preg_replace('`\[b\](.+)\[/b\]`isU', '<strong>$1</strong>', $var); 
$var = preg_replace('`\[i\](.+)\[/i\]`isU', '<em>$1</em>', $var);
$var = preg_replace('`\[u\](.+)\[/u\]`isU', '<u>$1</u>', $var);

$var = preg_replace('`(\[code].*?\[/code])`isU', $var2[1], $var);
$var = preg_replace('`\[code\](.+)\[/code\]`isU', '<div>$1</div>', $var);
return $var;
}

$text = 'Hello [b]newbie[/b], to write in bold, use the following [u]lol[/u] : [code][b](YOURTEXT) [u]lol[/u][/b][/code] [b][u]LOL[/u][/b]';

echo bbcode($text); 
?>

HOWEVER, there is a new problem left : if the character chain starts directly with '[code]' for example

[code][b]hello[/b][/code] test

than the result will be :

test test

This is because $var2[1] now leads to what comes after the [/code].

Could someone please help me to make a better delimitation that would also work for that second character chain ? Thank you in advance !

5
  • I'm not sure what you want to do... anyway, you are using greedy regex... you should modify (.+) to a lazy expression (.+?) Commented Jun 26, 2015 at 21:56
  • What I want to do is that the bbcode (in the example : [b](YOURTEXT)[/b]) inside [code] tag DOESN'T get transformed into <strong>(YOURTEXT)</strong>. The problem is that I don't know how to make a preg_replace for [code] that doesn't change all the bbcode that is inside that tag :( Commented Jun 26, 2015 at 23:57
  • The only idea I had was to make at the beginning of the function something that would transform '[' into ':beg:' and ']' into ':end:' and then at the bottom of the function retransform it into '[' and ']'. But the problem once again is that I don't know how to - ONLY - apply that to a specific caracter chain between 2 tags and ignoring the rest of the caracter chain. And there's probably a better way to do but I don't know how >< Commented Jun 27, 2015 at 0:06
  • split your string with (\[code].*?\[/code]), capture the delimiter with the PREG_SPLIT_DELIM_CAPTURE option, and apply the other replacements only on the even items of your array. But whatever you decide to do, you need to change your function to deal with nested tags. Commented Jun 27, 2015 at 0:43
  • Thanks Casimir for the PREG_SPLIT_DELIM_CAPTURE function but I didn't achieve making my script perfectly work yet (cf: edited post) Commented Jun 27, 2015 at 19:06

1 Answer 1

1

Finally, I solve all the problems I had with that :

<?
function bbcode($var) {
   $var2 = getStringBetween($var, '[code]', '[/code]');

   $var = preg_replace('`\[b\](.+)\[/b\]`isU', '<strong>$1</strong>', $var); 
   $var = preg_replace('`\[i\](.+)\[/i\]`isU', '<em>$1</em>', $var);
   $var = preg_replace('`\[u\](.+)\[/u\]`isU', '<u>$1</u>', $var);

   $var = preg_replace('`(\[code].+\[/code])`isU', '<div>'.$var2.'</div>', $var);
   return $var;
}

function getStringBetween($str,$from,$to)
{
    $sub = substr($str, strpos($str,$from)+strlen($from),strlen($str));
    return substr($sub,0,strpos($sub,$to));
}

$text = 'Hello [b]newbie[/b], to write in bold, use the following [u]lol[/u] : [code][b](YOURTEXT) [u]lol[/u][/b][/code] [b][u]LOL[/u][/b]';

echo bbcode($text); 
?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.