PHP to get all the pictures on the page address

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags address array code content copy data examples function

This article analyzes and introduces a variety of use of regular extraction of the map in the picture, see all the following examples I can not preg_match_all this function, there is a need for friends to refer to.
-
-
The code is copied as follows
<?php
Get all the picture 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;

}

?>

/<img.*srcs*=s*["|"? s* ([^> "' 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:

The code is copied as follows
Preg_match_all ("/<img.*srcs*=s*[" |)? s* ([^> "' s]*)/I", Str_ireplace ("", "", $content), $arr);

Oh my content part by PHP plus escaped, so I need to 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

The code is copied as follows
<?php
$ext = ' gif|jpg|jpeg|bmp|png ';//list image suffixes to achieve multiple extensions matching by http://www.jzread.com Green software
$str = "<p><img title=" Green Software "alt=" Green Software "onload=" Resizeimage (this,860) "src=" Http://www.jzread.com/data/soft _img/2010091101619.jpg "/></p><p><img title=" Green Software "alt=" Green Software "onload=" Resizeimage (this,860) "src=" "Http://www.jzread.com/data/soft_img/2010091029938.jpg"/></p><p><img title= "Green Software" alt= "Green Software" Onload= "Resizeimage" (this,860) "src=" http://www.jzread.com/data/soft_img/2010092839019.jpg "/></p>";
Preg_match_all ("/(HREF|SRC) = ([" | "]?) ([^ "' >]+.] ($ext)) 2/i ", $str, $matches);
Var_dump ($matches);
?>

Results

  Code Copy Code
Array (5) {
  [0]=>
  Array (3) {
    [0]=>
     string src= "http://www.jzread.com/data/soft_img/2010091101619.jpg"
    [1]=
    string src= "Http://www.jzread.com/data/soft_img/2010091029938.jpg"
     [2]=>
    string (src=) http://www.jzread.com/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) ""
&NBsp;
  [3]=>
  Array (3) {
    [0]=>
    string (Wuyi) http:// Www.jzread.com/data/soft_img/2010091101619.jpg "
    [1]=>
    String (51)" Http://www.jzread.com/data/soft_img/2010091029938.jpg
    [2]=>
    String (Wuyi) "Http://www.jzread.com/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 code is copied as follows
$newstext =preg_replace (Preg_replace (<img[^>]+srcs*=s*)? [^> "s]+"? [^>]*>) im ', ' <a href= ' $2″>$1</a> ', $newstext);

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:

$ refers to the text that is matched by the whole pattern;

Refers to the first () quoted string;

$ refers to the second () quoted string;

Related Article

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.