【问题标题】:PHP BB Code noparse tagPHP BB 代码 noparse 标签
【发布时间】:2013-07-06 19:21:34
【问题描述】:

我正在尝试编写一个 BB 解析器。 我的代码如下所示:

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

我想检查 $string 中是否有任何包含 noparse 标记的子字符串,并跳过它将解析该子字符串的其他标记的部分。现在我不知道该怎么做。 有什么建议么? 提前致谢

【问题讨论】:

    标签: php tags bbcode


    【解决方案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;
      }
    ?>
    

    【讨论】:

      猜你喜欢
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-23
      • 1970-01-01
      相关资源
      最近更新 更多