PHP is getting all the pictures of the page address _php tutorial

Source: Internet
Author: User
Get all the picture address of 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 need to remove the address of the nth image as the logo image of the article, the article code (the HTML of the content) is saved to a field in the database, and then the picture address is saved to another field. I use the regular solution above.

I explained that the above address is directly getting the value of the SRC attribute within the IMG tag. Use this regular PHP page to access the path if you can find a picture, you can use it directly, if not, you can use Preg_match_all to save all the addresses to the array, and then process the path, For example, get the file name (without the path part), then re-compose the URL, and then delete the picture.

My example:

Preg_match_all ("/" ' s]*)/I ", str_ireplace (" \ "," ", $content), $arr);

Oh my content part by PHP to add 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

$ext = ' gif|jpg|jpeg|bmp|png ';//list the image suffix to achieve multiple extension matching by http://www.bKjia.c0m Green software
$str = '

';
Preg_match_all ("/(HREF|SRC) = ([" | ']?) ([^ "' >]+. ($ext)) \2/i ", $str, $matches);
Var_dump ($matches);
?>


Results

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

PHP matches images and links to images

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


The difference between 1.preg_replace and str_replace:

Str_replace is just 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 entire pattern;

It refers to the first () quoted string;

The second () refers to a string;

Excerpt from PHP development

http://www.bkjia.com/PHPjc/478301.html www.bkjia.com true http://www.bkjia.com/PHPjc/478301.html techarticle PHP//Get all Picture address function getimages ($str) {$match _str =/((/HTTP) + ([^ rn () ^$! ' |[] {}]*) ((. gif) | (. jpg) | (. bmp) | (. png) | (. GIF) | (. JPG) | (. PNG) | (. BMP))/; Preg_m ...

  • 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.