无法让preg_match_all工作

时间:2013-05-15 09:59:21

标签: preg-match preg-match-all

我是正则表达式的新手,并且真的很难让它发挥作用。

我正在尝试从以下html之间的页面中获取一些信息:

<!--webbot bot="Include" U-Include="/inspections/Restaurants_Avalon.html" TAG="BODY" startspan --> EVERYTHING IN BETWEEN<!--webbot bot="Include" i-checksum="41417" endspan -->

我试过了:

$pattern = '/<.*?webbot bot=\"Include\" U-Include=\".*?\".*?startspan.*?(.*?)<.*?webbot bot=\"Include\" i-checksum=\".*?\" endspan.*?/i';

以及其他一些变化,但我明显缺乏经验和对常规表达的理解,只是创造了规律的混乱,而不是表达。

有人可以看看,告诉我我做错了什么吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

只需更改此部分:

startspan.*?(.*?)<.*?webbot

通过

startspan -->(.*?)<!--webbot

行动中:

$str = '<!--webbot bot="Include" U-Include="/inspections/Restaurants_Avalon.html" TAG="BODY" startspan --> EVERYTHING IN BETWEEN<!--webbot bot="Include" i-checksum="41417" endspan -->';

$pat = '/<.*?webbot bot=\"Include\" U-Include=\".*?\".*?startspan -->(.*?)<!--webbot bot=\"Include\" i-checksum=\".*?\" endspan.*?/i';

preg_match($pat, $str, $m);
print_r($m);

<强>输出:

Array
(
    [0] => <!--webbot bot="Include" U-Include="/inspections/Restaurants_Avalon.html" TAG="BODY" startspan --> EVERYTHING IN BETWEEN<!--webbot bot="Include" i-checksum="41417" endspan
    [1] =>  EVERYTHING IN BETWEEN
)