Home > Backend Development > PHP Tutorial > location.replace php preg_match_all combined with str_replace replaces all img in the content

location.replace php preg_match_all combined with str_replace replaces all img in the content

WBOY
Release: 2016-07-29 08:39:03
Original
1212 people have browsed it

There are a lot of javascript scripts and useless information in the collected image img tags. You must replace what you want, such as alt. First, let’s take a look at the content to be filtered. I copied it casually:

Copy code The code is as follows:


sdfsdfsdf”3568df.com靓图”sfsdfsdfasdfsadf”3568df.comsdfsadf”3568df.comsdfsdf


Replace the above into the shape like:

Copy the code The code is as follows:


”My where src=”http://www.xxx.com/upimg/080330 /120D1232295023X0.gif” src=”http://www.xxx. com/upimg/080330 /120D1232295023X0.gif" This address should be retained, because the pictures use the source address. The method is roughly: first read all the IMG tags in the content, and then extract the SRC of each IMG tag. , and combine it into its own content, and finally replace it.
preg_match_all is the function I want. It can create a three-dimensional array of content matched by regular expressions. You can traverse, search and replace them. If you don’t know much about it, please check the manual. I won’t introduce it in detail here. Function code:




Copy code

The code is as follows:function replace($str)

{

preg_match_all(”/]+>/isU”, $str, $arr);
for($i=0,$j=count($arr[0]);$i<$j;$i++){
$str = str_replace($arr[0][$i],””My”,$str);
}
return $str;
}


The above introduces location.replace php preg_match_all combined with str_replace to replace all img in the content, including location.replace content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
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