[英]PHP Find and replace certain string pattern
我在php $ string中有一段html代码。 问题是,我需要从中删除所有“ img”和“ a href”标签。 我想应该使用preg_replace函数,但是我的模式应该是什么样子?
我要替换:
"<img some params and address>" with ""
和
"<a href="some random address with unknown length">my text</a>" with "my text"
您可以将preg_replace与正则表达式一起使用。 像这样:
$text = preg_replace("/<img (.*?)>/i", "", $text);
$text = preg_replace("/<a (.*?)>(.*?)</a>/i", "$2", $text);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.