]*?src\s*=\s*(\'|\" )(.*?)\\1[^>]...>;" method to remove the picture."/> ]*?src\s*=\s*(\'|\" )(.*?)\\1[^>]...>;" method to remove the picture.">

Home  >  Article  >  Backend Development  >  How to remove images from content using php regular expressions

How to remove images from content using php regular expressions

藏色散人
藏色散人Original
2021-07-13 10:24:411952browse

php正则去掉内容中图片的方法:首先创建一个PHP示例文件;然后通过“preg_replace('/911a089107768724fb33c87b2462853f]*?src\s*=\s*(\'|\")(.*?)\\1[^>]...>;”方法去掉图片即可。

How to remove images from content using php regular expressions

本文操作环境:Windows7系统、PHP7.1版,DELL G3电脑

php 正则怎么去掉内容中的图片?

php正则删除img标签的方法。

具体如下:

一、问题

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

<div>欢迎访问php中文网<img src="https://www.php.cn/images/logo.gif" /></div>

要求删除后变成:

<div>欢迎访问php中文网</div>

二、解决方法:

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

运行结果:

<div>欢迎访问php中文网</div>

推荐学习:《PHP视频教程

The above is the detailed content of How to remove images from content using php regular expressions. 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