开发者

use preg match and replace to replace li tags within ol tags

Here is what i got:

<ol>
<li></li>
<li></li>
</ol>

<ul>
<li></li>
<li></li>
</ul>


if (preg_match("/<ol>.*(<li(|\s*\/)>).*<\/ol>/Ums", $text->Bodytext)) {
$cleanlist = preg_replace("/(<li(|\s*\/)>)/", "<li><tag>", $text->Bodytext);

This php code works well if the page only contains ol tags, but if i开发者_运维问答t contains both ul and ol tags for some reason it changes li tags inside ul tags too and I only want it to replace it within ol tags. What could be the problem and how can i fix it?


The problem in your regex is likely the greedyness. You should append a question mark to .*? so it matches less padding.

It would however be more reliable it you didn't try to accomplish it with a single regex. That's always a bit more involving then going the simple route:

$html = preg_replace_callback('#<ol>(.*?)</ol>#Us', "change_li", $html);
function change_li($m) {
    return preg_replace('#<li>#', '<li><tag>', $m[0]);
}

Now before the meme posters come around, using regular expressions is not the most reliable approach. It's workable in your case unless you have wacky HTML. An alternative would be to use phpQuery or QueryPath however, where matching is as simple as qp($html)->find("ol")->find("li"). Though the actual replacing would be more involving with that approach.


In PHP 5.3 this task could be done as follows:

        $input = <<<END
<ol>
<li></li>
<li></li>
</ol>

<ul>
<li></li>
<li></li>
</ul>
END;



        $result = preg_replace_callback(
                "/(<ol>.*<\/ol>)/Ums",
                function ($ol) {
                    return preg_replace("/(<li(|\s*\/)>)/", "<li><tag>", $ol[1]);                    
                },
                $input
        );

        var_dump($result);

The output would be:

        string '<ol>
<li><tag></li>
<li><tag></li>
</ol>

<ul>
<li></li>
<li></li>
</ul>' (length=72)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜