Use the preg_replace function to match images and add links

Source: Internet
Author: User
Use the preg_replace function to match images and add links

  1. $ Con = file_get_contents ("http://bbs.it-home.org/wzjs ");
  2. $ Pattern = "/<[img | IMG]. *? Src = [\ '| \ "] (. *? (? : [\. Gif | \. jpg | \. png]) [\ '| \ "]. *? [\/]?> /";
  3. $ New_con = preg_replace ($ pattern, "$0", $ con );
  4. Echo $ new_con;
  5. ?>

Note: $0 indicates the matching content. $1 indicates the content of the first () match. $2 indicates the content of the second () match, and so on! In this way, you can add a link to the image. if you are interested, try it yourself.

Articles you may be interested in: php code for matching image addresses: a PHP regular expression matching date and time (timestamp conversion) example php matches the regular expression of any Web site php regular expression rewrite html image img path code example PHP regular expression match get URL domain name code php match preg_match_all match picture php regular expression match domain name in URL

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.