I'm trying to write a BB Parser. My code looks like that:

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

I want to check if there are any substrings in $string that contain the noparse tag and skip the part where it would parse the other tags for that substring. Now i have no idea how to do that. Any suggestions? Thanks in advance

有帮助吗?

解决方案

Try this, hope it helps

<?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;
  }
?>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top