开发者

php preg_match_all: split list items and child lists

I'm trying to split some html content using php's preg-match-all function:

<li class="cat-item"><a title="blabla" href="#">parent 1</a>
    <ul class="children">
        <li class="cat-item"><a title="" href="#"&开发者_开发知识库gt;child 1</a></li>
    </ul>
</li>
<li class="cat-item cat-item-4"><a title="blabla" href="#">father 2</a>
    <ul class="children">
        <li class="cat-item"><a title="" href="#">child 1</a></li>
        <li class="cat-item"><a title="bla" href="#">child 2</a></li>
    </ul>
</li>

I want to be able to change the link description, for example;

<a title="" href="#">child 1</a>

to

<a title="" href="#">I changed that</a>

while keeping the structure of the original html. so far, I succeeded to split the links using :

$results = preg_match_all('/<a\s[^>]*href\s*=\s*(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>/siU', $html, $tokens); 

foreach ( $tokens[0] as $category)
{
    echo $category.'<br>';
}

the drawback of this is that it discards child lists, and outputs all the list items in the same level; no distinction between parent and child.

any idea to keep original hierarchy?

thanx :)


use preg_replace to replace strings! something like this here:

$output = preg_replace("/^([123]0|[012][1-9]|31)(\\.|-|\/|,)(0[1-9]|1[012])(\\.|-|\/)(19[0-9]{2}|2[0-9]{3})$/","$1",$in_nn_date);

where $1 or $2 is the thing you have searched with regex and grouped in.

best would be that you use some online editor or something... like this one

and try there! hope it helps...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜