code example for PHP to get all the image addresses of a page

Source: Internet
Author: User
This article mainly introduces the PHP regular access page All the image address of the relevant information, the need for friends can refer to the following

<?php//get all the pictures on the page address function getimages ($str) {$match _str = "/((/HTTP) + ([^ rn () ^$! '" ' |[] {}<>]*) (. gif) | (. JPG) | (. bmp) | (. png) | (. GIF) | (. JPG) | (. PNG) | (. BMP))/";p Reg_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

<?php$ext = ' gif|jpg|jpeg|bmp|png ';//list the image suffix to achieve multiple extension matching by http://www.jb51.net green software $str = ' <p> </p><p></p><p></p> ';p Reg_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 (Wuyi)" 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 matches images and links to images

$newstext =preg_replace (Preg_replace ('/(]+srcs*=s* "? [^> "s]+)"? [^>]*>)/im ', ' <a href= ' $2″>$1</a> ', $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;

About PHP is to get all the picture address of the page knowledge, small series to introduce to you 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.