Home >Backend Development >PHP Tutorial >PHP regular method to obtain all image addresses on the page_PHP tutorial

PHP regular method to obtain all image addresses on the page_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:50:04834browse

//Get all 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 take out the address of the Nth picture as the logo picture of the article. The article code (html of the content) is saved to a field in the database, and then the picture address is saved to another field. I just use the above regular expression. Solved.

Let me explain that the above address is to directly obtain the value of the src attribute in the img tag. If you can access the path on the php page using this regular expression, you can use it directly. If not, you can use preg_match_all to match all The address is first saved into an array, and then the path is processed, such as getting the file name (without the path part), then reorganizing the url, and then deleting the image.

My example:

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

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


Example

$ext = 'gif|jpg|jpeg|bmp|png';//List image suffixes to achieve multi-extension matching by http://www.bKjia.c0m Green software
$str = '

Green Software

Green Software

Green Software

';
preg_match_all("/(href|src)=(["|']?)([^ "'>]+.($ext))2/i", $str, $matches);
var_dump($matches);
?>


Results

array(5) {
[0]=>
array(3) {
[0]=>
String(57) "src="http://www.bKjia.c0m /data/soft_img/2010091101619.jpg""
[1]=>
String(57) "src="http://www.hzhuti.com/sonyericsson/w715/ 2010091029938.jpg""
[2]=>
String(57) "src="http://www.bKjia.c0m /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.c0m /data/soft_img/2010091101619.jpg"
[1]=>
String(51) "http://www.bKjia.c0m /data/soft_img/2010091029938.jpg"
[2]=>
String(51) "http://www.bKjia.c0m /data/soft_img/2010092839019.jpg"
}
[4]=>
array(3) {
[0]=>
String(3) "jpg"
[1]=>
String(3) "jpg"
[2]=>
String(3) "jpg"
}
}

Detailed explanation of PHP regular matching of images and adding links to images

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


1. The difference between preg_replace and str_replace:

str_replace is just pure character replacement, while preg_replace is regular replacement

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

$0 refers to the text matched by the entire pattern;

$1 refers to the string referenced by the first ( );

$2 refers to the string referenced by the second (); and so on

Excerpted from php development

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478301.htmlTechArticle?php //Get all image addresses on the page function getimages($str) { $match_str = /((http ://)+([^ rn()^$!`|[]{}]*)((.gif)|(.jpg)|(.bmp)|(.png)|(.GIF)|( .JPG)|(.PNG)|(.BMP)))/; preg_m...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn