Php regular expression to get all image addresses on the page

Source: Internet
Author: User
Tags php regular expression
Php regular expression to get all the image addresses on the page & lt ;? Php retrieves all the image addresses on the page: functiongetimages ($ str) {$ match_str () + ([^ rn () ^ $! '| [] {}& Lt; & gt;] * (.gif) | (.jpg) | (.bmp) | (.png) | (. GIF) | (. JPG) | (. PNG) | (. BMP) php regular expression to get all the image addresses on the page

 ] * Certificate (.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 I need to retrieve the address of the nth image as the logo image of the article. the article code (html of the content) is saved to a field in the database, then save the image address to another field. I used the above regular expression to solve the problem.

In my note, the above address is to directly obtain the value of the src attribute in the img label. access this path on the php page using this regular expression. if you can find the image, you can directly use it. if not, you can use preg_match_all to save all the addresses to the array and then process the path, for example, you can obtain the file name (excluding the path), recompose the url, and delete the image.

My example:

preg_match_all("/"'s]*)/i",str_ireplace("\","",$content),$arr);

The content is escaped by php, so I need to remove it first, str_ireplace ("\", "", $ content ), then save the matched content to the $ arr array (two-dimensional ).
$ Arr [1] is the array that stores the path.


Instance

 

'; Preg_match_all ("/(href | src) = ([" |']?) ([^ "'>] +. ($ Ext) \ 2/I", $ str, $ matches); var_dump ($ matches);?>


Result

array(5) {  [0]=>  array(3) {    [0]=>    string(57) "src="http://www.111cn.net /data/soft_img/2010091101619.jpg""    [1]=>    string(57) "src="http://www.hzhuti.com/sonyericsson/w715/ 2010091029938.jpg""    [2]=>    string(57) "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(51) "http://www.111cn.net /data/soft_img/2010091101619.jpg"    [1]=>    string(51) "http://www.111cn.net /data/soft_img/2010091029938.jpg"    [2]=>    string(51) "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 regular expression matching image and add link details to the image

$newstext=preg_replace(preg_replace('/(]+srcs*=s*”?([^>"s]+)”?[^>]*>)/im', ‘$1', $newstext); 


1. differences between preg_replace and str_replace:

Str_replace is a replacement of only characters, while preg_replace is a replacement of regular expressions.

2. $0, $1, $2, and so on:

$0 indicates the text that is matched by the entire pattern;

$1 indicates the first () referenced string;

$2 refers to the second () referenced string, and so on.

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.