PHP preg_match结果为null

时间:2013-06-12 08:10:27

标签: php regex preg-match expression

$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文件中无法执行

1 个答案:

答案 0 :(得分: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>

不,不是,&gt;div class=&#039;...,因为您拨打了htmlentitiescheck the docs here
此外,借助the DOMDocument object,恕我直言

,解析这样的html会更容易