php使用preg_match_all匹配文章中的圖片,_PHP教程

來源:互聯網
上載者:User

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


有時候我們需要使用php擷取網頁中的圖片並儲存下來,這裡我們可以使用preg_match_all用來匹配圖片地址,需要的碼農可以參考下

preg_match_all 函數:

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

例子:

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

結果:

Array(    [0] => Array        (            [0] =>             [1] =>             [2] =>         )    [1] => Array        (            [0] => http://www.xxx.com/usr/themes/dddefault/images/logo.png            [1] => http://www.xxx.com/usr/uploads/2012/09/531656480.jpg            [2] => http://www.xxx.com/usr/uploads/2012/09/2647136297.jpg        ))

原文地址:http://www.manongjc.com/article/708.html

相關閱讀:

php preg_match_all結合str_replace替換內容中所有img

php preg_match與preg_match_all 函數區別分析

php preg_match的匹配不同國家語言執行個體

PHP使用preg_match_all導致apache崩潰問題的解決方案

php preg_match正則匹配中的/u、/i、/s含義

php preg_match函數匹配的字串長度的問題

php中preg_match isU表示什麼

php preg_match_allRegex函數使用講解

php preg_matchRegex函數執行個體講解

javascript Regextest()和exec()用法

php Regex中的非貪婪模式比對

php Regex效率 貪婪、非貪婪與回溯分析

javascript Regex貪婪模式與非貪婪模式

Javascript 中Regex的全域匹配模式分析

javascript Regex匹配字串string

js 驗證(判斷)是否為網址

phpRegex函數使用執行個體

php preg_match函數和php preg_match_al函數l執行個體,方法,例子

php preg_match_all()函數使用執行個體

php preg_matchRegex函數執行個體

http://www.bkjia.com/PHPjc/1121388.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1121388.htmlTechArticlephp使用preg_match_all匹配文章中的圖片, 有時候我們需要使用php擷取網頁中的圖片並儲存下來,這裡我們可以使用preg_match_all用來匹配圖片地...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.