0

I have a lot of different blog posts in a database. They are in markdown. I want to convert them to html. I have problems with the ul tag.

For instance in a simplified way I have that:

Something
- First
- Second
Something else

Second ul
- First
- Second
Some final text

I could put the li tags:

$text = preg_replace("/^- (.*)/m", "<li>$1</li>", $text);

But how can I identify the beginning or end of the list to put the ul tag?

I want this result:

Something
<ul>
<li>- First</li>
<li>- Second</li>
</ul>
Something else

Second ul
<ul>
<li>- First</li>
<li>- Second</li>
</ul>
Some final text
tripleee
  • 139,311
  • 24
  • 207
  • 268
Nrc
  • 8,463
  • 14
  • 50
  • 95

1 Answers1

0

You need to do this in two passes, so with two preg_replace calls:

$text = preg_replace("/^- (.*)/m", "<li>$1</li>", 
            preg_replace("/^- .*(\R- .*)*/m", "<ul>\n$0\n</ul>", $text)
        );

The inner one executes first, and finds lines that starts with - and captures all the following lines that also start with - until the line that follows does not start -.

.* matches anything up to the end of the current line. The new line character is not matched by .. So the next \R is used for matching anything considered a linebreak sequence. And then again the hyphen check is done and the remainder of the line is captured. This can repeat (( ... )*) as many times as possible.

That matched block is then wrapped in an ul tag. The hyphens are not removed at this stage.

The outer one will wrap lines that start with - in li tags, taking out the -.

trincot
  • 211,288
  • 25
  • 175
  • 211
  • Explain your inner one, please. What is \R? – Nrc Mar 30 '17 at 15:58
  • I added more detail. – trincot Mar 30 '17 at 16:53
  • I have been searching and I could not find any reference about \R is this like a generic that encompass \n and \r ? – Nrc Mar 31 '17 at 10:12
  • There is a hyperlink on [`\R`](http://perldoc.perl.org/perlrebackslash.html#Misc) in my answer ;-) See also [this answer](http://stackoverflow.com/questions/18988536/php-regex-how-to-match-r-and-n-without-using-r-n/18992691#18992691). – trincot Mar 31 '17 at 11:11