Php 预标记中的新行?
我正在使用nbbc.sourceforge.net解析bbcode标记。您可以在此处看到实际问题: 当我解析文本时,其中包括编程语言的bbcode,例如php规则,它将php bbcode标记替换为pre标记:Php 预标记中的新行?,php,bbcode,Php,Bbcode,我正在使用nbbc.sourceforge.net解析bbcode标记。您可以在此处看到实际问题: 当我解析文本时,其中包括编程语言的bbcode,例如php规则,它将php bbcode标记替换为pre标记: $bb = new BBCode; $bb->AddRule('php',array( 'simple_start'=>'<pre>', 'simple_end'=>'</pre>', 'allow_in'=>fa
$bb = new BBCode;
$bb->AddRule('php',array(
'simple_start'=>'<pre>',
'simple_end'=>'</pre>',
'allow_in'=>false,
));
',
'allow_in'=>false,
));
回显'.$bb->解析($_POST['content'])。';
?>
更新
(1)
我尝试使用回调:
$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);
但不管怎么说,这里有一些中断:
更新(2)
。我就是这么做的,但我不认为这是最终的解决方案:
trim()
应删除前导和尾随空格,包括换行符,而不影响用户开始输入文本后的换行符。您能通过trim()
传递[php][/php]
块的内容吗
或者,在重读问题后,在其中插入一个
str\u replace(“\n\n”,“\n”,$input)
以将双换行替换为单换行?库太长;我没读过。你在那里打开了一个问题,所以给它一些时间,让作者来处理它。我知道,我只是认为有可能在preg_replace解析后处理内容,只在pre中删除中断。但是,工作比以往任何时候都糟糕。不要在PHP5.2中使用它,我的意思是,preg_replace_callback
函数它不会工作。更新了我的问题。不是很有帮助,但我认为这是正确的方法。
$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']);
$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>';
$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);