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 に答える