Bulleted list from dashes Let's keep this simple.

Input:

- abc
- def
- ghi


Desired output (whitespace optional):

&lt;ul&gt;
    &lt;li&gt;abc&lt;/li&gt;
    &lt;li&gt;def&lt;/li&gt;
    &lt;li&gt;ghi&lt;/li&gt;
&lt;/ul&gt;


Current code:

$output = preg_replace_callback(
    "((^- (.*)$\r*\n*)+)m",
    function($m) {
        return "&lt;ul&gt;&lt;li&gt;".
            implode(
                "&lt;/li&gt;&lt;li&gt;",
                preg_split(
                    "(^- )m",
                    substr($m[0],2)
                )
            ).
            "&lt;/li&gt;&lt;/ul&gt;";
    },
    $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?
Wouldn't this give you the desired output:

$result = '&lt;ul&gt;' . preg_replace('/^- (.*)$/m', '&lt;li&gt;$1&lt;/li&gt;', $subject) . '&lt;/ul&gt;';


Update:

$output = preg_replace_callback(
    "((^- (.*)$\r*\n*)+)m",
    function($m) {
        return '&lt;ul&gt;' . preg_replace('/^- (.*)$/m', '&lt;li&gt;$1&lt;/li&gt;', $m) . '&lt;/ul&gt;';
    },
    $input
);