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 !

  • I'm not sure what you want to do... anyway, you are using greedy regex... you should modify `(.+)` to a lazy expression `(.+?)` – Federico Piazza Jun 26 '15 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 (YOURTEXT). 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 :( – Northwinter Jun 26 '15 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 > – Northwinter Jun 27 '15 at 00: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. – Casimir et Hippolyte Jun 27 '15 at 00:43
  • Thanks Casimir for the PREG_SPLIT_DELIM_CAPTURE function but I didn't achieve making my script perfectly work yet (cf: edited post) – Northwinter Jun 27 '15 at 19:06

1 Answers1

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); 
?>