php regex bbcode returns blank
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;
}
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);
链接地址: http://www.djcxy.com/p/29874.html
上一篇: 替换特定BBCode标记之间的所有换行符
下一篇: PHP正则表达式bbcode返回空白