This article describes how to obtain all the image addresses on the PHP Regular Expression page. For more information, see
<? 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.php.net 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.php.net /data/soft_img/2010091101619.jpg""[1]=>string(57) "src="http://www.hzhuti.com/sonyericsson/w715/ 2010091029938.jpg""[2]=>string(57) "src="http://www.php.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.php.net /data/soft_img/2010091101619.jpg"[1]=>string(51) "http://www.php.net /data/soft_img/2010091029938.jpg"[2]=>string(51) "http://www.php.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.
For more information about how to use PHP regular expressions to obtain all the image addresses on the page, I hope to help you!