Preg_replace is used to search and replace regular expressions. if only strings are matched, str_replace () is recommended. because of its high execution efficiency, preg_replace is used to search and replace regular expressions, we recommend that you use str_replace () to match strings, because it is much more efficient.
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:
The code is as follows:
$ Con = file_get_contents ("http://www.jb51.net /");
$ Pattern = "/<[img | IMG]. *? Src = [\ '| \ "] (. *? (? : [\. Gif | \. jpg | \. png]) [\ '| \ "]. *? [\/]?> /";
$ New_con = preg_replace ($ pattern, "$0", $ 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.