PHP regular expression to obtain all image addresses on the page ,. PHP regular expression gets all the image addresses on the page. php gets all the image addresses on the page. functiongetimages ($ str) {$ match_str () + ([^ rn () ^ $! '| [] {}] * )(.Gif) | (.jpg) | (.bmp) | (. pn PHP regular expression to obtain all the image addresses on the page,
<? Php // get all the image addresses on the page 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 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
<? Php $ ext = 'gif | jpg | jpeg | bmp | png '; // list Image suffixes to achieve multi-extension matching by http://www.bkjia.com green Software $ str =''; 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.bkjia.com /data/soft_img/2010091101619.jpg""[1]=>string(57) "src="http://www.hzhuti.com/sonyericsson/w715/ 2010091029938.jpg""[2]=>string(57) "src="http://www.bkjia.com /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.bkjia.com /data/soft_img/2010091101619.jpg"[1]=>string(51) "http://www.bkjia.com /data/soft_img/2010091029938.jpg"[2]=>string(51) "http://www.bkjia.com /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.
For more information about how to use PHP regular expressions to obtain all the image addresses on the page, I hope to help you!
Articles you may be interested in:
- PHP extracts the image address from the string [regular expression]
- Php regular expression extraction Image address
- Example of PHP regular expression extraction that does not contain the image address of the specified URL
- Php regular expression matches the remote image address in the article and downloads the image to the local device.
Response, php // get all the image addresses on the page function getimages ($ str) {$ match_str = "/(http: //) + ([^ rn () ^ $! '"' | [] {}] * (.Gif) | (.jpg) | (.bmp) | (. pn...