Use the preg_replace function to match images and add links

WBOY
Release: 2016-07-25 09:05:45
Original
1048 people have browsed it
  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. ?>
Copy code

Explanation: $0 represents this matching content, $1 represents the content matched by the first (), $2 represents the content matched by the second (), and so on! In this way, a link is added to the picture. Friends who are interested may wish to test it by themselves.

Articles you may be interested in: An example of PHP code matching image address Example of PHP regular matching of date and time (timestamp conversion) php regular expression matching any URL An example of php regular matching code that rewrites the img path of html images PHP regular matching to obtain the code of the domain name in the URL php uses preg_match_all to match pictures in articles php regular expression matching domain name in URL



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!