PHP regular Get page All picture address _php instance

Source: Internet
Author: User
<?php
//Get page All Picture 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 I need to take out the address of the nth picture as a logo picture of the article, the article code (content of the HTML) saved to a database field, and then the picture address to save to another field. I just use the above regular solution.

I explained that the above address is a direct fetch of the value of the SRC attribute within the IMG tag. Access the path using the regular PHP page if you can find a picture, you can use it directly, if not, you can use Preg_match_all to save all addresses to an array and then process the path, For example, get the file name (excluding the path portion), then recreate the URL, and then delete the picture.

My example:

 
 

Oh my content part by the PHP plus 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 image suffixes to achieve multiple extensions matching by http://www.jb51.net green software
$str = ' <p>< IMG title= "Green software" alt= "Green Software" "onload=" Resizeimage (this,860) "src=" http://www.jb51.net/data/soft_img/2010091101619. JPG "/></p><p></p><p></p>";
Preg_match_all ("/(HREF|SRC) = ([" | "]?) ([^ "' >]+.] ($ext)) \2/i ", $str, $matches);
Var_dump ($matches);

Results

Array (5) {
[0]=>
Array (3) {
[0]=>
string () "Src=" http://www.jb51.net/data/soft_img/ 2010091101619.jpg ""
[1]=>
string ($) "src=" http://www.hzhuti.com/sonyericsson/w715/2010091029938.jpg "
[2]=>
string" src= "Http://www.jb51.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.jb51.net/data/soft_img/2010091101619.jpg "
[1]=>
string" http://www.jb51.net/data/soft_img/ 2010091029938.jpg "
[2]=>
string" Http://www.jb51.net/data/soft_img/2010092839019.jpg
}
[4]=>
Array (3) {
[0]=>
string (3) "JPG"
[1]=>
string (3) "JPG"
[2]= >
String (3) "JPG"
}
}

PHP to match the picture and to add detailed links to the picture

 
 

The difference between 1.preg_replace and str_replace:

Str_replace is only 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 whole pattern;

Refers to the first () quoted string;

$ refers to the second () quoted string;

About PHP is to get all the pictures of the page address knowledge, small series to introduce to everyone here, I hope to help you!

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.