0

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

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

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

4

1 回答 1

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;
  }
?>
于 2013-07-06T19:34:30.770 回答