我已经是一个字符串设置高亮$mybbcode = [b]Hello word[/b]
用PHP我想在html页面html格式来显示它。
例如:< div><b>hello word</b><div>
Answer 1:
基本上,别人已经给你后说,但如果你在谷歌搜索,你会看到很多quicky有关的信息,和done功能。 下面是一个例子:
function bbc2html($content) {
$search = array (
'/(\[b\])(.*?)(\[\/b\])/',
'/(\[i\])(.*?)(\[\/i\])/',
'/(\[u\])(.*?)(\[\/u\])/',
'/(\[ul\])(.*?)(\[\/ul\])/',
'/(\[li\])(.*?)(\[\/li\])/',
'/(\[url=)(.*?)(\])(.*?)(\[\/url\])/',
'/(\[url\])(.*?)(\[\/url\])/'
);
$replace = array (
'<strong>$2</strong>',
'<em>$2</em>',
'<u>$2</u>',
'<ul>$2</ul>',
'<li>$2</li>',
'<a href="$2" target="_blank">$4</a>',
'<a href="$2" target="_blank">$2</a>'
);
return preg_replace($search, $replace, $content);
}
只为懒惰的程序员;)
我邀请你搜索并决定哪些是从已经完成对你的项目的所有代码的最佳。
Answer 2:
你将不得不使用正则表达式来BBCodes转换为HTML: http://www.php.net/manual/en/ref.pcre.php
例如 :
$string = preg_replace('#\[b\](.+)\[\/b\]#iUs', '<b>$1</b>', $string);
文章来源: Show bbcode in html page with php