Heim  >  Artikel  >  Backend-Entwicklung  >  location.replace php preg_match_all结合str_replace替换内容中所有img

location.replace php preg_match_all结合str_replace替换内容中所有img

WBOY
WBOYOriginal
2016-07-29 08:39:031143Durchsuche

采集回来的图片img标签中,有好多javascript脚本和无用的信息,必需过替换自己想要的,比如alt。先看看要过滤的内容,我随便复制出来:

复制代码 代码如下:


sdfsdfsdf”3568df.com靓图”sfsdfsdfasdfsadf”3568df.com靓图sdfsadf”3568df.com靓图sdfsdf


要把上面替换成形如:

复制代码 代码如下:


”我的信息” 其中src=”http://www.xxx.com/upimg/080330 /120D1232295023X0.gif” src=”http://www.xxx.com/upimg/080330 /120D1232295023X0.gif”这个地址要保留,因为图片用的都是源地址


方法大致是:先读取内容里的所以IMG标签,然后把每个IMG标签的SRC抽取出来,并且组合成自己的内容,最后进行替换。
preg_match_all就是我想要的函数,它能够把正则表达式匹配到的内容建立一个三维数组,你可以对它们进行遍历查找替换,不太了解的请查查手册,这里不作具体介绍。函数代码:

复制代码 代码如下:


function replace($str)
{
preg_match_all(”/location.replace php preg_match_all结合str_replace替换内容中所有img]+>/isU”, $str, $arr);
for($i=0,$j=count($arr[0]);$i$str = str_replace($arr[0][$i],”\”我的信息””,$str);
}
return $str;
}

以上就介绍了location.replace php preg_match_all结合str_replace替换内容中所有img,包括了location.replace方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn