views:

111

answers:

2

I was reading http://github.github.com/github-flavored-markdown/

I would like to implement that "Newline modification" in PHP Markdown:

Best I could think of is:

$my_html = Markdown($my_text);
$my_html = preg_replace("/\n{1}/", "<br/>", $my_html);

But this runs very unexpectable.

+1  A: 

PHP's nl2br -function doesn't cut it?

nl2br — Inserts HTML line breaks before all newlines in a string

http://php.net/manual/en/function.nl2br.php

If you also want to remove all linebreaks (nl2br inserts <br/>), you could do:

str_replace('\n', '', nl2br($my_html));

If not, please elaborate on how your solution fails, and what you'd like to fix.

TuomasR
nl2br or my preg_replace tends to add too many line breaks, eg. outside paragraphs
Lauri
A: 

I've came up with the following solution, imitating most parts of the gfm newline behavior. It passes all the relevant tests on the page mentioned in the original post. Please note that the code below preprocesses markdown and outputs flavored markdown.

preg_replace('/(?<!\n)\n(?![\n\*\#\-])/', "  \n", $content);
nperson