php
  • regex
  • preg-match-all
  • 2010-02-25 59 views 2 likes 
    2

    我想構建一個從imdb列表中獲取電影URL的爬蟲。我可以將頁面上的所有鏈接都放到一個數組中,並且只想選擇那些帶有「標題」的鏈接。在PHP和正則表達式中使用preg_match_all的URL匹配

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

    其中$pattern='/title/'

    我收到以下錯誤:

    Warning: preg_match_all() [function.preg-match-all]: Delimiter must not be alphanumeric or backslash in C:\xampp\htdocs\phpProject1\index.php on line 53

    如何去這個任何想法?非常感謝。

    回答

    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

    1

    確定$pattern'/title/'的時候preg_match_all叫?

    當您提供給preg_match_all(第1個參數)的模式沒有正確分隔時,您收到錯誤。

    相關問題