php Regex提取圖片url程式

來源:互聯網
上載者:User

我想對 html 的圖片進行提取.

 代碼如下 複製代碼

<img ico src="/noavatar_small.gif" />

<img src="/noavatar_small.gif" />

如上地址. 我想全部提取出來 但是包含'ico' 的地址 忽略. 求正則 , 就是有些圖片 提取.有些不提取.

例子:

 代碼如下 複製代碼
<s*imgs+[^>]*?srcs*=s*('|")(.*?)\1[^>]*?/?s*>

經改進後..

正確解答如下

 代碼如下 複製代碼

/<img(?:(?!(ico)).)+/>/

執行個體

 代碼如下 複製代碼

//要替換的內容   
    $content = '<img alt="" src="js/fckeditor/UserFiles/image/F201005201210502415831196.jpg" width="600" height="366"><br><br><br><br><img alt="" src="js/fckeditor/UserFiles/image/33_avatar_middle.jpg" width="120" height="120">';

    //提取圖片路徑的src的Regex
    preg_match_all("/<img(.*)src="([^"]+)"[^>]+>/isU",$content,$matches);

            $img = "";
            if(!empty($matches)) {
            //注意,上面的Regex說明src的值是放在數組的第三個中
                $img = $matches[2];
            }else {
                $img = "";
            }
            if (!empty($img)) {
                $img_url = "http://".$_SERVER['SERVER_NAME'];

                $patterns= array();
                $replacements = array();

                foreach($img as $imgItem){

                $final_imgUrl = $img_url.$imgItem;
                $replacements[] = $final_imgUrl;

                $img_new = "/".preg_replace("///i","/",$imgItem)."/";
                $patterns[] = $img_new;

                }

                //讓數組按照key來排序
                ksort($patterns);
                ksort($replacements);

                //替換內容
                $vote_content = preg_replace($patterns, $replacements, $content);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.