2013-02-07 194 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確定。但我只想回答這個問題 –