Home >Backend Development >PHP Tutorial >PHP regular method to delete img tag

PHP regular method to delete img tag

怪我咯
怪我咯Original
2017-07-05 10:43:372336browse

这篇文章主要介绍了php正则删除img标签的方法,结合具体实例形式分析了php针对img标签正则匹配相关操作技巧,需要的朋友可以参考下

本文实例讲述了php正则删除img标签的方法。分享给大家供大家参考,具体如下:

一、问题

正则抓取过程中需要删除正文中的img标签,如:

<p>欢迎访问PHP中文网<img src="http://www.www.vn/images/logo.gif" /></p>

要求删除后变成:

<p>欢迎访问PHP中文网</p>

二、解决方法:

$str='<p>欢迎访问PHP中文网<img src="http://www.www.vn/images/logo.gif" /></p>';
$str= preg_replace('/<\s*img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', '', $str);
echo $str;

运行结果:

<p>欢迎访问PHP中文网</p>

The above is the detailed content of PHP regular method to delete img tag. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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