-1

I have this bbcode tag "remover" which should remove bbcode tags from my test text. All i get is nothing. Just blank page where should be the text replaced with html tags. Whats wrong with it. And maybe anyone have some better script to share.

$str = 'This [b]is just[/b] a [i]test[/i] text!';
function forum_text($str)
{
$str = htmlspecialchars($str);

$str = preg_replace( "#\[url\](?:http:\/\/)?(.+?)\[/url\]#is", "<a href=\"http://$1\">$1</a>", $str ); 
$str = preg_replace( "#\[img\](?:http:\/\/)?(.+?)\[/img\]#is", "<img src=\"http://$1\" />", $str ); 
$str = preg_replace( "#\[b\](.+?)\[/b\]#is", "<strong>$1</strong>", $str ); 
$str = preg_replace( "#\[i\](.+?)\[/i\]#is", "<i>$1</i>", $str ); 
$str = preg_replace( "#\[u\](.+?)\[/u\]#is", "<u>$1</u>", $str ); 

return $str;
}
mypoint
  • 73
  • 2
  • 9
  • 1
    What is the value of `$str` after each `preg_replace()`? – Matt Aug 27 '12 at 18:34
  • If you're using `#` as your delimiter, you don't need to escape the `/` characters. FYI. – Matt Aug 27 '12 at 18:34
  • Have you turned on error_reporting? You're aware that it doesn't "remove" bbcodes, but replace them with HTML? There's also a difference between `return` and `echo` (but that depends on the invoking code, that you haven't shown). – mario Aug 27 '12 at 18:36
  • Also, don't use regex to parse HTML. [You'll end up summoning Cthulhu](http://stackoverflow.com/a/1732454/1338999). – Matt Aug 27 '12 at 18:36
  • This code works for me, daft question - are you actually printing `forum_text($str)` after declaring the function? – Tieran Aug 27 '12 at 18:38
  • Thats all my code for test purpose. Maybe I am doing something wrong. I tried with return/echo/print. Nothing works for me. I have empty page. Error reporting is on and it doesnt show anything. – mypoint Aug 27 '12 at 18:39
  • You've declared the function that will format your code for you, but you then need to actually call it with your string and output the results. Try adding `print(forum_text($str));` and see if that works. – Tieran Aug 27 '12 at 18:41
  • @Tieran still nothing. Just blank page. – mypoint Aug 27 '12 at 18:43
  • Have you put it outside the function declaration, i.e. after the `}`? If you're new to functions it may be worth reading up a little so you understand how it all works - http://www.php.net/manual/en/functions.user-defined.php. – Tieran Aug 27 '12 at 18:45
  • No I have not put it outside. For me it just shows blank. Nothing is there. I have no idea why! – mypoint Aug 27 '12 at 18:51

1 Answers1

0

The following is your code, with some code in front of it (to make sure any errors are shown) and some code at the back (that actually calls your function).

If this doesn't work for you, your problem is not here, unless you don't have a working PCRE.

error_reporting(-1); ini_set('display_errors', 'On');

$str = 'This [b]is just[/b] a [i]test[/i] text!';
function forum_text($str)
{

    $str = htmlspecialchars($str);

    $str = preg_replace( "#\[url\](?:http:\/\/)?(.+?)\[/url\]#is", "<a href=\"http://$1\">$1</a>", $str );
    $str = preg_replace( "#\[img\](?:http:\/\/)?(.+?)\[/img\]#is", "<img src=\"http://$1\" />", $str );
    $str = preg_replace( "#\[b\](.+?)\[/b\]#is", "<strong>$1</strong>", $str );
    $str = preg_replace( "#\[i\](.+?)\[/i\]#is", "<i>$1</i>", $str );
    $str = preg_replace( "#\[u\](.+?)\[/u\]#is", "<u>$1</u>", $str );

    return $str;
}

echo forum_text($str);
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • @mypoint just keep in mind that a function declaration doesn't run by itself, you MUST call it from outside of the declaration :) – Ja͢ck Aug 27 '12 at 19:07