php中使用preg_match_all匹配文章中的圖片

helong3314發表於2021-09-14

preg_match_all 函式:

int preg_match_all ( string pattern, string subject, array matches [, int flags] )執行一個全域性正規表示式匹配
在 subject 中搜尋所有與 pattern 給出的正規表示式匹配的內容並將結果以 flags 指定的順序放到 matches 中。
搜尋到第一個匹配項之後,接下來的搜尋從上一個匹配項末尾開始。
flags 可以是下列標記的組合(注意把 PREG_PATTERN_ORDER 和 PREG_SET_ORDER 合起來用沒有意義):
PREG_PATTERN_ORDER 對結果排序使 $matches[0] 為全部模式匹配的陣列,$matches[1] 為第一個括號中的子模式所匹配的字串組成的陣列,以此類推!

    $pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/";
    $con = file_get_contents("https://www.jb51.net/news/jb-1.html");
    preg_match_all($pattern,$con,$match);
  print_r($match);

Laravel

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章