【问题标题】:URL matching using preg_match_all in PHP and regex在 PHP 和正则表达式中使用 preg_match_all 进行 URL 匹配
【发布时间】:2011-01-21 00:14:18
【问题描述】:

我正在尝试构建一个从 imdb 列表中获取电影网址的爬虫。我能够将页面上的所有链接放入一个数组中,并且只想选择那些带有“标题”的链接。

preg_match_all($pattern, "[125] => href=\"/chart/2000s?mode=popular\" [126] => href=\"/title/tt0111161/\" ", $matches);

$pattern='/title/'.

我收到以下错误:

警告:preg_match_all() [function.preg-match-all]:第 53 行 C:\xampp\htdocs\phpProject1\index.php 中的分隔符不能是字母数字或反斜杠

关于如何解决这个问题的任何想法?非常感谢。

【问题讨论】:

    标签: php regex preg-match-all


    【解决方案1】:

    使用DOM Parser

    // Create DOM from URL or file
    $html = file_get_html('http://www.example.com/');
    
    // Find all links containing title as part of their HREF 
    $links = $html->find('a[href*=title]');
    
    // loop through links and do stuff
    foreach($links as $link) { 
           echo $element->href . '<br>';
    }
    

    http://simplehtmldom.sourceforge.net/manual.htm

    【讨论】:

      【解决方案2】:

      您确定在调用 preg_match_all 时 $pattern'/title/' 吗?

      当提供给 preg_match_all(第一个参数)的模式没有正确分隔时,您会遇到错误。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多