2013-02-07 143 views

回答

0

我beleive本文将帮助你解决问题...

http://thesinkfiles.hubpages.com/hub/Regex-for-BBCode-in-PHP

function parseCode($txt) 
{ 
    // these functions will clean the code first 
    $ret = strip_tags($txt); 

    // code replacements 
    $ret = preg_replace('#\[b\](.+)\[\/b\]#iUs', '<b>$1</b>', $ret); 
    $ret = preg_replace('#\[link\=(.+)\](.+)\[\/link\]#iUs', '<a href="$1">$2</a>', $ret); 
    $ret = preg_replace('#\[img\](.+)\[\/img\]#iUs', '<img src="$1" alt="Image" />', $ret); 
    $ret = preg_replace('#\[quote\=(.+)\](.+)\[\/quote]#iUs', '<div class="quote">$2</div><div class="quote-by">By: $1</div>', $ret); 


    // return parsed string 
    return $ret; 
} 
0

您可以测试它

$str= preg_replace('~\[img\](.*)\[\/img\]~si', '<img src="$1" alt="$1">', $str); 
+0

请保持我ñ记住,你需要这个命令之前清空'$ str'以避免严重的安全漏洞。 (['ヶ辆($ STR)'](http://php.net/manual/en/function.htmlentities.php)) – Yogu

+0

@Yogu确定。但我只想回答这个问题 –