【问题标题】:Having trouble getting preg_match_all to work无法让 preg_match_all 正常工作
【发布时间】:2013-05-15 09:59:21
【问题描述】:

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

我正在尝试从以下 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';

还有其他几十种变体,但我明显缺乏对正则表达式的经验和理解,这只会造成常规混乱而不是表达式。

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

谢谢!

【问题讨论】:

    标签: preg-match preg-match-all


    【解决方案1】:

    只需更改这部分:

    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
    )
    

    【讨论】:

    • 没用。只收到一个空数组。数组 ( [0] => 数组 ( ) [1] => 数组 ( ) )
    • 也试过了:$pattern = '/(.+?)/is';
    • 刚刚注意到我没有在引号前面包含 \。添加后仍然无法正常工作。
    • @ShannonCole:为我工作,请参阅我的编辑。你是怎么用的?
    猜你喜欢
    • 1970-01-01
    • 2012-09-05
    • 2017-11-06
    • 2011-09-06
    • 1970-01-01
    • 2023-03-10
    • 2020-10-16
    • 2014-01-15
    • 2019-07-29
    相关资源
    最近更新 更多