Description: preg_replace: searches and replaces regular expressions. If you only want to match strings, we recommend that you use str_replace () because of its high execution efficiency.
Mixed preg_replace (mixed pattern, mixed replacement, mixed subject [, int limit])
Search for matches in pattern in subject and replace them with replacement. If limit is specified, only limit matches are replaced. If limit is omitted or its value is-1, all matches are replaced.
As mentioned above, php uses preg_match_all to match the image in the article. The following code matches the image and adds the link:
Copy codeThe Code is as follows: <? Php
$ Con = file_get_contents ("http://www.jb51.net /");
$ Pattern = "/<[img | IMG]. *? Src = [\ '| \ "] (. *? (? : [\. Gif | \. jpg | \. png]) [\ '| \ "]. *? [\/]?> /";
$ New_con = preg_replace ($ pattern, "<a href = '$ 1'> $0 </a>", $ con );
Echo $ new_con;
?>
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.