【发布时间】:2013-10-21 18:02:17
【问题描述】:
我有一个模式被匹配到一个大字符串,使用 preg_match_all,它得到了正确的匹配,但是它似乎进入每个匹配并尝试找到更多匹配,然后进入每个匹配,然后当它最终在一个空字符串上时停止。听起来像是某种递归,但我不需要也不想这样做,有没有办法阻止它?
感谢您的帮助!
function getCategories($source)
{
$categories = array();
$pattern = "~<span class=.*\n<table class=.*\n<tr>\n<th.*\n<.th>\n<th.*\n<.th>\n<th.*\n<.th>\n<th.*\n<.th>\n<th.*\n<.th>\n<th.*\n<.th><.tr>\n(<tr id=.*\n(.*\n){6}<.td><.tr>(<.table>)?\n)*~";
preg_match_all($pattern, $source, $categories);
return $categories;
}
$categories = getCategories($source);
print_r($categories);
【问题讨论】:
-
是的,您可以通过不在 html 上使用正则表达式来修复它,而是改用 DOM/xpath。
-
多么丑陋的正则表达式......只需使用类似phpQuery
-
我几乎终于让我的代码在某种程度上工作了,我没有时间花时间尝试理解新类并重写它,有没有办法只删除这个递归函数的性质?
标签: php regex recursion preg-match-all