PHP正则表达式:在Html中匹配特定单词

PHP正则表达式:在Html中匹配特定单词,php,regex,preg-match,preg-match-all,Php,Regex,Preg Match,Preg Match All,我有以下html代码: <html> <div class="the_grp"> <h3>heading <span id="sn-sin" class="the_decs">(keyword: <i>cat</i>)</span></h3> <ul> <li> <div> <div><span cl

我有以下html代码:

<html>
<div class="the_grp">
<h3>heading <span id="sn-sin" class="the_decs">(keyword: <i>cat</i>)</span></h3>
<ul>
    <li>
        <div>
            <div><span class="w_pos"></span></div>
            <div class="w_the">
            <a href="http://www.exampledomain.com/20111/cute-cat">cute cat</a>, 
            <a href="http://www.exampledomain.com/7456/catty">catty</a>, 
            </div>
        </div>
    </li>   
    <li>
        <div>
            <div><span class="w_pos"></span></div>
            <div class="w_the">
            <a href="http://www.exampledomain.com/7589/sweet">sweet</a>, 
            <a href="http://www.exampledomain.com/10852/sweet-cat">sweet cat</a>, 
            <a href="http://www.exampledomain.com/20114/cat-vs-dog">cat vs dog</a>, 
        </div>
    </li>
</ul>
</div>

<a id="ant"></a>
<div class="the_grp">
<h3>another heading <span id="sn-an" class="the_decs">(ignore this: <i>cat</i>)</span></h3>
<ul>
    <li>
        <div>
            <div><span class="w_pos"></span></div>
            <div class="w_the"><a href="http://www.exampledomain.com/118/bad-cat">bad cat</a></div>
        </div>
    </li>
</ul>
</div>

标题(关键字:cat)
  • , ,
  • , , ,
我想匹配html代码中的以下单词:

  • 可爱的猫
  • 甜蜜的
  • 甜猫
  • 猫对狗
我使用这种模式并捕获[2]来获取这些单词:

#<a href="http\:(.*?)">(.*?)<\/a>#i
#(.*)#i
我的php代码如下所示:

preg_match_all('#<a href="http\:(.*?)">(.*?)<\/a>#i', $data, $matches);
echo '<pre>';
print_r($matches[2]);
echo '</pre>';
preg#u match_all('#(.*)#i',$data,$matches);
回声';
打印($matches[2]);
回声';
那个图案也和“坏猫”很相配。如何捕捉以下单词:可爱的猫,猫,甜的,甜的猫,猫对狗


提前感谢。

最好只使用HTML解析器。下面是您如何使用

最好是
file\u get\u html
,它将基本上调用file\u get\u内容和
str\u get\u html

str\u get\u html
是将字符串解析为简单html dom对象的方法

foreach($html->find('a') as $element) 
    if ($element->plaintext != "bad cat")
       echo $element->plaintext  . '<br>';

我将参考不要使用正则表达式来解析HTML。您使用的模式将匹配
a
中的所有内容。你要做的事情是抓取,只需找一个PHP库就可以了。@MikeVelazco我以前使用过简单的html dom,我仍然找不到解决方案,因为这些单词都在同一个div类中。我不是正则表达式专家,但你可以用
(可爱的猫|猫|甜的猫|甜的猫|猫对狗)替换第二个
(.*)
foreach($html->find('a') as $element) 
    if ($element->plaintext != "bad cat")
       echo $element->plaintext  . '<br>';