我正在尝试在菜单上执行preg_match。但是php似乎一直在跳过第一场比赛。我找不到原因。
<div id="subnav">
<div class="wrap">
<ul id="menu-patternstutorials" class="nav superfish">
<li id="menu-item-11512" class="menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-home current-menu-ancestor menu-item-11512">
<a href="http://localhost/Sites/craftpassion/">Patterns Tutorials</a>
<ul class="sub-menu">
<li id="menu-item-11506" class="menu-item menu-item-type-taxonomy menu-item-object-category current-menu-ancestor current-menu-parent menu-item-11506 star-li-bg">
<a title=" TEST" href="http://localhost/Sites/craftpassion/category/needle-craft/sewing">Sewing</a>
<ul class="sub-menu">
<li id="menu-item-11508" class="menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-home current-menu-ancestor current-menu-parent menu-item-11508">
<a href="http://localhost/Sites/craftpassion/">Basic Techniques</a>
<ul class="sub-menu">
PHP
$pattern = '#<ul[^`]*?>[\s]*?<li [^`]*?>[\s]*?<a[^`]*?>([^`]*?)</a>[\s]*?<ul[^`]*?>#i';
preg_match($pattern, $menu, $matches);
我在期待:
<ul id="menu-patternstutorials" class="nav superfish">
<li id="menu-item-11512" class="menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-home current-menu-ancestor menu-item-11512">
<a href="http://localhost/Sites/craftpassion/">Patterns Tutorials</a>
<ul class="sub-menu">
但继续得到:
<ul class="sub-menu">
<li id="menu-item-11506" class="menu-item menu-item-type-taxonomy menu-item-object-category current-menu-ancestor current-menu-parent menu-item-11506 star-li-bg">
<a title=" TEST" href="http://localhost/Sites/craftpassion/category/needle-craft/sewing">Sewing</a>
<ul class="sub-menu">
为什么它不匹配第一个预期的?
答案 0 :(得分:0)
HTML!=常规语言。参见:
答案 1 :(得分:0)
不要使用正则表达式解析(X)HTML。由于您没有处理常规语言(这是常规表达式能够处理的语言),因此无法(正确地)执行此操作。请改用DOM或SAX解析器。