Let's keep this simple.
Input:
- abc
- def
- ghi
Desired output (whitespace optional):
<ul>
<li>abc</li>
<li>def</li>
<li>ghi</li>
</ul>
Current code:
$output = preg_replace_callback(
"((^- (.*)$\r*\n*)+)m",
function($m) {
return "<ul><li>".
implode(
"</li><li>",
preg_split(
"(^- )m",
substr($m[0],2)
)
).
"</li></ul>";
},
$input
);
It works, the output is exactly what I want, but it seems kinda... hackish. Having a preg_split
inside a preg_replace_callback
seems a bit redundant, but I can't seem to find a way of capturing subpatterns that are inside a quantifier. If I could do that then I would implode
the subpatterns rather than having to split the matched part again.
Any suggestions?