Home > Article > Backend Development > location.replace php preg_match_all combined with str_replace replaces all img in the content
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:
sdfsdfsdfsfsdfsdfasdfsadfsdfsadfsdfsdf
Copy the code The code is as follows:
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:
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],””,$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.