Use php to handle all img tags in html code

Source: Internet
Author: User
Php is used to process all img tags in a piece of html code. there is a piece of html code that contains many tags and a lot of img tags. now, you need to extract the src Image addresses of all img tags in php language, and process them through a program to generate the image addresses under your server, replace the original src Image address with the address processed by our program. Make sure that all img tags in html code are processed by php in the image.
There is an html code that contains many tags and many img tags. now, you need to extract the src Image addresses of all img tags in php, after some program processing, the image address under my server is generated, and the original src Image address is replaced with the address processed by my program.
Make sure that the img tag after the image address is replaced is the same as the original img tag in the html code.
Php html image sharing:
------ Solution --------------------
This can be implemented using regular expressions.

$ Url = "http://www.ggggggg.com ";

// The file_get_contents () function reads the entire file into a string.

$ String = file_get_contents ($ url );

// The preg_match_all function matches the global regular expression.

Preg_match_all ("/] *) \ s * src = ('
------ Solution --------------------
\ ") ([^ '\"] + )('
------ Solution --------------------
\")/",

$ String, $ matches); // enclosed by quotation marks

// Preg_match_all ("/] *) \ ssrc = ([^ \ s>] +)/", $ string, $ matches); // no quotation marks

$ New_arr = array_unique ($ matches [0]); // Remove repeated values from the array

Foreach ($ new_arr as $ key ){

Echo $ key ."
";

}

?>

All captured images are displayed in the code running result.
------ Solution --------------------
$ String = 'Your content ';
Preg_match_all ("/] *) \ s * src = ('
------ Solution --------------------
\ ") ([^ '\"] + )('
------ Solution --------------------
\ ")/", $ String, $ matches );
$ New_arr = array_unique ($ matches [2] [0]); // Remove repeated values from the array
Foreach ($ new_arr as $ key => $ val ){
$ Picurl = get_name ($ val); // address of the processed image
Str_replace ($ val, $ picurl, $ string );
}
Function get_name ($ pic_item, $ path = 'upload /')
{
$ Pic_name = paipath.get_unique().'.jpg '; // name the timestamp in microseconds.
$ Write_fd = @ fopen ($ pic_name, "wb ");
@ Fwrite ($ write_fd, CurlGet ($ pic_item ));
@ Fclose ($ write_fd );
Return $ pic_name;
}

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.