Home >Backend Development >PHP Problem >How to use regular expression to delete a tag in php

How to use regular expression to delete a tag in php

王林
王林Original
2020-04-28 17:37:073412browse

How to use regular expression to delete a tag in php

目的:

使用正则表达式删除下列内容中的a标签,同时保留a标签内容

<div>欢迎来到php中文网<a href=//www.php.cn>www.php.cn</a></div>

修改后:

<div>欢迎来到php中文网www.php.cn</div>

解决方法:

$str = "<div>欢迎来到php中文网<a href=//www.php.cn>www.php.cn</a></div>";
$str = htmlspecialchars_decode($str);
$str = preg_replace("/]*>(.*?)<\/a>/is", "$1", $str);
echo $str;

运行结果为:

<div>欢迎来到php中文网www.php.cn</div>

更多相关教程请访问php中文网

The above is the detailed content of How to use regular expression to delete a tag in php. 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