I am trying to change the default behavior of Markdown where single line breaks are not converted to <br />.
I found that github flavored markdown does this, but I am having a hard time converting the rube gsub function to something similar in PHP.
The Ruby code looks like this:
text.gsub!(/(\A|^$\n)(^\w[^\n]*\n)(^\w[^\n]*$)+/m) do |x|
x.gsub(/^(.+)$/, "\\1 ")
end
But even that I try this simple approach in PHP I see no difference
$text = preg_replace("/^(.+)$/", "\\1", $text);
$text = Markdown($text);
Any help in the conversion from Ruby to PHP will be highly appreciated.
PS. No need for discussion of the behavior in general here, please see the meta question should the markdown renderer treat a single line break as br
You are missing two things in your attempt to translate the Ruby code:
gsub!calls it once for every match of its regex parameter, wih the match passed asx. Note the outer regex is a multiline one (as denoted by the finalm).<br />tags. Your regex just replaces its match by … itself.