Php regular expression to extract Image URLs

Source: Internet
Author: User
Php regular expression to extract the image url program. Use a regular expression to obtain the IMG tag, extract the SRC of each IMG tag, combine them into your own content, and finally replace the image I want to extract from html. for example, the code first uses a regular expression to obtain the IMG tag, then extracts the SRC of each IMG tag, combines it into its own content, and finally replaces it.

I want to extract html images.

 

The above address. I want to extract all the addresses that contain 'ico 'and ignore them. for correction, some images are extracted. Some images are not extracted.

Example:

 
  ]*?srcs*=s*('|")(.*?)\1[^>]*?/?s*>
 

After improvement ..

The correct answer is as follows:

//

Instance

// Content to be replaced $ content ='



'; // Extract the regular expression preg_match_all ("/] +>/isU", $ content, $ matches); $ img = ""; if (! Empty ($ matches) {// note that the above regular expression indicates that the src value is placed in the third of the array $ img = $ matches [2];} else {$ img = "";} if (! Empty ($ img) {$ img_url = "http ://". $ _ SERVER ['server _ name']; $ patterns = array (); $ replacements = array (); foreach ($ img as $ imgItem) {$ final_imgUrl = $ img_url. $ imgItem; $ replacements [] = $ final_imgUrl; $ img_new = "/". preg_replace ("// I", "/", $ imgItem ). "/"; $ patterns [] = $ img_new;} // sort the array by key ($ patterns); ksort ($ replacements ); // replace content $ vote_content = preg_replace ($ patterns, $ replacements, $ content );

Related articles:

How to use php regular expressions to extract html tags

Php code example for extracting html tags

Use php to extract HTML tags

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.