php - New lines in a pre tag? -
i'm using nbbc.sourceforge.net bbcode tags parsing. actual issue can see here: github. nbbc issue #1.
when i'm parsing text, includes bbcode programming language, example rule php, replaces php bbcode tag pre tag:
$bb = new bbcode; $bb->addrule('php',array( 'simple_start'=>'<pre>', 'simple_end'=>'</pre>', 'allow_in'=>false, ));
i have empty lines in pre, divides normal lines, here picture of this:
if i'll use $bb->setignorenewlines(true);
new lines not exists in text not in pre. how 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 test, copy textarea test new lines in code while 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)
tried 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, did, dont think 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 nbbc library issue 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);
trim()
should remove leading , trailing whitespace, including newline, without affecting newlines after user started entering text. can pass contents of [php][/php]
block through trim()
?
or, after re-reading question, slip str_replace("\n\n", "\n", $input)
in there replace double newlines single newlines?
Comments
Post a Comment