PHP is getting all the picture addresses on the page

Source: Internet
Author: User

<?php//get all the pictures on the page address function getimages ($str) {    $match _str = "/((/HTTP) + ([^ rn () ^$! '" ' |[] {}<>]*) (. gif) | (. JPG) | (. bmp) | (. png) | (. GIF) | (. JPG) | (. PNG) | (. BMP))/";    Preg_match_all ($match _str, $str, $out, preg_pattern_order);    return $out;}? >

/ "' s]*)/I
, I use Kindeditor to save the article, but need to remove the address of the nth image as the logo image of the article, the article code (the HTML of the content) is saved to a field in the database, and then the picture address is saved to another field. I use the regular solution above.

I explained that the above address is directly getting the value of the SRC attribute within the IMG tag. Use this regular PHP page to access the path if you can find a picture, you can use it directly, if not, you can use Preg_match_all to save all the addresses to the array, and then process the path, For example, get the file name (without the path part), then re-compose the URL, and then delete the picture.

My example:

Preg_match_all ("/ "' s]*)/I", str_ireplace ("\", "", $content), $arr);

Oh my content part by PHP to add escaped, so I need to first remove, str_ireplace ("\", "", $content), and then save the matching content to the $arr array (two-dimensional).
$arr [1] is the array that stores the path.


Instance

<?php$ext = ' gif|jpg|jpeg|bmp|png ';//list the image suffix to achieve multiple extension matching by http://www.111cn.net  green software $str = ' <p></p><p></p><p></p> ';p Reg_match_all ("/(HREF|SRC) = ([ "|"?) ([^ "' >]+. ($ext)) \2/i ", $str, $matches); Var_dump ($matches);? >


Results

Array (5) {  [0]=>  Array (3) {    [0]=>    string (page) "Src=" http://www.111cn.net/data/soft_img/ 2010091101619.jpg ""    [1]=>    string (src=) http://www.hzhuti.com/sonyericsson/w715/2010091029938.jpg ""    [2]=>    string "src=" Http://www.111cn.net/data/soft_img/2010092839019.jpg ""  }  [1]=>  Array (3) {    [0]=>    string (3) "src"    [1]=>    string (3) "src"    [2]=>    string ( 3) "src"  }  [2]=>  Array (3) {    [0]=>    string (1) "" "    [1]=>    string (1)" ""    [2]=>    String (1) "" "  }  [3]=>  Array (3) {    [0]=>    string (Wuyi)" HTTP/ Www.111cn.net/data/soft_img/2010091101619.jpg "    [1]=>    string (Wuyi)" Http://www.111cn.net/data/soft_ Img/2010091029938.jpg "    [2]=>    string (Wuyi)" Http://www.111cn.net/data/soft_img/2010092839019.jpg "  }  [4]=>  Array (3) {    [0]=>    string (3) "JPG"    [1]=>    string (3) "JPG"    [2]=>    string (3) "JPG"  

PHP matches images and links to images


The difference between 1.preg_replace and str_replace:

Str_replace is just a pure character substitution, and Preg_replace is a regular replacement

Description of 2.$0,$1,$2, etc.:

"$" refers to the text that is matched by the entire pattern;

It refers to the first () quoted string;

The second () refers to a string;

PHP is getting all the picture addresses on the page

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.