预标签中的新行?

时间:2013-10-07 11:31:26

标签: php bbcode

我正在使用nbbc.sourceforge.net进行bbcode标记解析。您可以在此处看到实际问题:Github. NBBC Issue #1.

当我解析文本时,包括编程语言的bbcode,例如php的规则,将php bbcode标记替换为pre标记:

$bb = new BBCode;

$bb->AddRule('php',array(
    'simple_start'=>'<pre>',
    'simple_end'=>'</pre>',
    'allow_in'=>false,
));

我在pre中有一个空行,它划分了法线,这里的图片是:

empty lines in a pre

如果我将使用$bb->SetIgnoreNewlines(true);,则新行不会出现在不在pre中的文本中。如何解决?

<!DOCTYPE html">
<html">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>NBBC</title>
    </head>
    <body>
    <style>
    pre { margin: 2px; border: 1px solid #c2c2c2; width: 400px; }
    div.content { border: 4px dotted #27BFC5; margin: 2px; padding: 4px; width: 407px; }
    </style>
        <?php 

            require_once('nbbc-master/nbbc.php');

            $bb = new BBCode;

            // $bb->SetIgnoreNewlines(true);

            $bb->AddRule('php',array(
                'simple_start'=>'<pre>',
                'simple_end'=>'</pre>',
                'allow_in'=>false,
            ));

            echo '<div class="content">'.$bb->Parse($_POST['content']).'</div>';

        ?>

<!-- Content for test, just copy it into the textarea
Test new lines in code

While it work.
Great!
[php]

$a ="test A";

$b =$a;
$c =$b;

[/php]
End Tests!
-->

        <form action="" method="post">
            <textarea name="content" cols="50" rows="13"></textarea> <br />
            <input type="submit" value="submit" name="submit" />
        </form>
    </body>
</html>

更新 (1)我尝试使用回调:

$content = preg_replace_callback('/(.*\[php\])(.*)(\[\/php\])(.*)/is',function($matches){
                return trim($matches[1]).str_replace("\n\n","\n",trim($matches[2])).trim($matches[3]).trim($matches[4]);
            },$_POST['content']);

但无论如何这些都会打破:

breakets

更新 (2)。嗯,这就是我所做的,但我不认为这是最终的解决方案:

$bb = new BBCode;

            // $bb->SetIgnoreNewlines(true);

            $bb->AddRule('php',array(
                'simple_start'=>'<pre>',
                'simple_end'=>'</pre>',
                'allow_in'=>false,
            ));

            $content = preg_replace_callback('/(.*\[php\])(.*)(\[\/php\].*)/is',
            function ( $matches ) { return $matches[1].trim($matches[2]).$matches[3]; }
            ,$_POST['content']);

            $content = $bb->Parse($_POST['content']);

            $content = preg_replace_callback('/(.*<pre>)(.*)(<\/pre>.*)/is',
            function ( $matches ) { return trim($matches[1]).preg_replace('/<br \/>/is','',trim($matches[2])).trim($matches[3]); }
            ,$content);

            echo '<div class="content">'.$content.'</div>';

对于NBBC图书馆,这个问题仍然存在。

更新 (3)站在此:

$content = $bb->Parse($content);

        $content = preg_replace_callback('/(.*<pre.*?>)(.*)(<\/pre>.*)/is',
        function ( $matches ) { 
            return trim($matches[1]).str_replace('<br />','',trim($matches[2])).trim($matches[3]); 
        }
        ,$content);

1 个答案:

答案 0 :(得分:1)

trim()应删除前导空格和尾随空格,包括换行符,而不会在用户开始输入文本后影响换行符。你能否通过[php][/php]传递trim()块的内容?

或者,在重新阅读问题之后,在str_replace("\n\n", "\n", $input)中插入一个换行符来替换双换行符?