dqellle310167 2013-10-07 11:31
浏览 44
已采纳

预标签中的新行?

I'm using nbbc.sourceforge.net for bbcode tags parsing. Actual issue you can see here: Github. NBBC Issue #1.

When I'm parsing a text, that includes bbcode for programming language, for example rule for php, that replaces a php bbcode tag into the pre tag:

$bb = new BBCode;

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

I have an empty lines in a pre, that divides the normal lines, here the picture of this:

empty lines in a pre

if I'll use the $bb->SetIgnoreNewlines(true); than new lines will not exists in a text that not in a pre. How to fix that?

<!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>

update (1) I tried to use callback:

$content = preg_replace_callback('/(.*\[php\])(.*)(\[\/php\])(.*)/is',function($matches){
                return trim($matches[1]).str_replace("

","
",trim($matches[2])).trim($matches[3]).trim($matches[4]);
            },$_POST['content']);

but these breaks anyway there:

breakets

update (2). Well, this is what I did, but I dont think that this is a final solution:

$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>';

For the NBBC library this issue is still on hold.

update (3) stand on this:

$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条回答 默认 最新

  • dongpinken0498 2013-10-07 13:33
    关注

    trim() should remove leading and trailing whitespace, including newline, without affecting newlines after the user started entering text. Can you just pass the contents of the [php][/php] block through trim()?

    Or, after re-reading the question, slip a str_replace(" ", " ", $input) in there to replace double newlines with single newlines?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?