2013-07-06 50 views
0

我正在嘗試寫一個BB Parser。 我的代碼看起來像這樣:PHP BB代碼noparse標籤

$string = preg_replace("/\[B\](.*)\[\/B\]/Usi", "<b>\\1</b>", $string); 
$string = preg_replace("/\[I\](.*)\[\/I\]/Usi", "<i>\\1</i>", $string); 
.... 

我想檢查是否有在$字符串包含noparse標籤,並跳過它會分析該字符串的其他標籤的一部分的任何字符串。現在我不知道該怎麼做。 有什麼建議嗎? 在此先感謝

回答

1

試試這個,希望它有助於

<?php 
    $text = bbcode("sometext"); 
    print_r($text); 

    function bbcode($text = null) { 
    /** Replace the bbcode tags inside [noparse] to something else **/ 
    $text = preg_replace('#\[noparse\](.*)\[/noparse\]#sUe', 'noparse(\'$1\')', $text); 

    $text = preg_replace("(\[b\](.+?)\[\/b])is", '<strong>$1</strong>', $text); 
    $text = preg_replace("(\[i\](.+?)\[\/i\])is", '<em>$1</em>', $text); 
    // and so on.............. 

    /** Now restore the bbcodes tags to its original format, which we were replaced earlier **/ 
    $text = str_replace(array('*NoParse1*', '*NoParse2*'), array('[', ']'), $text); 

    return $text; 
    } 

    function noparse($text = null) { 
    $text = str_replace(array('[', ']'), array('*NoParse1*', '*NoParse2*'), $text); 
    return $text; 
    } 
?> 
+0

謝謝!它工作得很好:) – Kable