【问题标题】:PHP preg_match results nullPHP preg_match 结果为空
【发布时间】:2013-06-12 08:10:27
【问题描述】:
$pattern = '/^<div\sclass="yui3-u-2-3"><div\sid=left><h2\sclass="ehr">(.*?)<\/h2>/';
$subject = htmlentities($search, ENT_QUOTES, "UTF-8");
$arslt=preg_match( $pattern, $subject,$matches);
if($arslt==1)
echo 'delete';
 else
echo $arslt;
print_r($matches);

以上代码结果

0Array ( )

$search 即我的主题是:-

<div class="yui3-u-2-3"><div id=left><h2 class="ehr">Suggestions</h2><div class="post"><ul><li class="hindiresult">See the <a href="/help">help</a> to know how search results can be improved</li><li class="hindiresult">Ask in our <a href="/forums">forum</a></li><li class="hindiresult">Send us <a href="/contact">email</a></li></ul></div><h2 class="ehr">Definitions of Chaise</h2><h3>noun</h3><ol class="wnol"><li class="hindiresult">a long chair; for reclining</li><li class="hindiresult">a carriage consisting of two wheels and a calash top; drawn by a single horse</li></ol></div></div><div class="yui3-u-1-3"><div id="right"><div class="adbox"><script type="text/javascript"><!-- google_ad_client = "pub-7708924170316014";google_ad_slot="0743863500";google_ad_width=300;google_ad_height=250; //--></script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script></div></div></div>

我已经在线测试并得到正确答案结果1。 http://www.phpliveregex.com/p/xu 并且可能其他网站我未能在我的 php 文件中执行

【问题讨论】:

  • /.*class="ehr">(.*)

标签: php regex preg-match expression


【解决方案1】:

$search 即我的主题是:-

<div class="yui3-u-2-3"><div id=left><h2 class="ehr">Suggestions</h2><div class="post"><ul><li class="hindiresult">See the <a href="/help">help</a> to know how search results can be improved</li><li class="hindiresult">Ask in our <a href="/forums">forum</a></li><li class="hindiresult">Send us <a href="/contact">email</a></li></ul></div><h2 class="ehr">Definitions of Chaise</h2><h3>noun</h3><ol class="wnol"><li class="hindiresult">a long chair; for reclining</li><li class="hindiresult">a carriage consisting of two wheels and a calash top; drawn by a single horse</li></ol></div></div><div class="yui3-u-1-3"><div id="right"><div class="adbox"><script type="text/javascript"><!-- google_ad_client = "pub-7708924170316014";google_ad_slot="0743863500";google_ad_width=300;google_ad_height=250; //--></script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script></div></div></div>

不,不是,它是&amp;gt;div class=&amp;#039;...,因为你调用了htmlentitiescheck the docs here
此外,借助the DOMDocument object,恕我直言,像这样解析html会更容易

【讨论】:

    猜你喜欢
    • 2011-05-18
    • 2013-11-29
    • 2016-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    相关资源
    最近更新 更多