php preg_match_all:拆分列表项和子列表
我正在尝试使用php的php preg_match_all:拆分列表项和子列表,php,html,hyperlink,preg-match-all,listitem,Php,Html,Hyperlink,Preg Match All,Listitem,我正在尝试使用php的preg match all函数拆分一些html内容: <li class="cat-item"><a title="blabla" href="#">parent 1</a> <ul class="children"> <li class="cat-item"><a title="" href="#">child 1</a></li> </
preg match all
函数拆分一些html内容:
<li class="cat-item"><a title="blabla" href="#">parent 1</a>
<ul class="children">
<li class="cat-item"><a title="" href="#">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>
例如,我希望能够更改链接描述
<a title="" href="#">child 1</a>
到
同时保持原始html的结构。
到目前为止,我成功地使用以下方法拆分了链接:
$results = preg_match_all('/<a\s[^>]*href\s*=\s*(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>/siU', $html, $tokens);
foreach ( $tokens[0] as $category)
{
echo $category.'<br>';
}
$results=preg\u match\u all('/]*href\s*=\s*(\“?)([^\“>]*?)\\1[^>]*>(.*)/siU',$html,$tokens);
foreach($tokens[0]作为$category)
{
回声$category.“
”;
}
这样做的缺点是它丢弃子列表,并在同一级别输出所有列表项;父母和孩子之间没有区别
有没有保留原有等级制度的想法
thanx:)使用preg\u replace替换字符串!这里是这样的:
$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);
其中$1或$2是您使用regex搜索并分组的内容
最好是你使用一些在线编辑器或其他东西。。。像
试试看!希望它能有所帮助……对于regexp来说,这是一项非常困难的工作,在这种情况下,使用HTML解析器会更好(我非常支持regexp,通常情况下会遇到困难,但在这种嵌套上,这是一项艰难的工作)。你到底想在锚中添加什么?thanx Orbling,我同意在解析html时,regexp不是正确的方法,我开始使用domDocument,但到目前为止还没有达到我的目的。。我想实现的是wordpress的wp_list_categories()函数;我需要翻译回应的结果;这个函数没有array或object的返回值,只有echo:(因此我尝试创建一个dirthy修复:)Thanx FeRtoll,我想我需要更多的正则表达式来解决这个问题,因为我需要重新创建原始的输出层次结构。
$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);