我正在尝试使用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>
</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>至
<a title="" href="#">I changed that</a>同时保持原始html的结构。到目前为止,我成功地使用以下命令拆分了链接:
$results = preg_match_all('/<a\s[^>]*href\s*=\s*(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>/siU', $html, $tokens);
foreach ( $tokens[0] as $category)
{
echo $category.'<br>';
}这样做的缺点是它丢弃了子列表,并输出同一级别的所有列表项;没有区分父项和子项。
有没有保留原有层次结构的想法?
谢谢:)
发布于 2011-01-27 08:02:19
使用preg_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是您使用正则表达式搜索并分组的内容。
最好是你使用一些在线编辑器或者别的什么.像this one一样
试一试那里!希望这能帮上忙。
https://stackoverflow.com/questions/4811553
复制相似问题