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:
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("\n\n","\n",trim($matches[2])).trim($matches[3]).trim($matches[4]);
},$_POST['content']);
but these breaks anyway there:
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);