对于大多数人来说,这可能很简单,但我一直在努力从字符串中替换所有<a href="url">
任何</a>
。
有人可以帮我解决这个问题并给我一些关于文档和示例的指示吗? php.net没有多大帮助,难以理解。
据我所知,我需要更换:
'<a href=*' up until the '>' character is met.
替换</a>
我可以管理:)
答案 0 :(得分:3)
在PHP中使用strip_tags函数会出现什么问题?你是否绝对试图使用正则表达式(我在使用HTML时会建议反对)?
PHP文档示例正是您想要的:
<?php
$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo strip_tags($text);
echo "\n";
// Allow <p> and <a>
echo strip_tags($text, '<p><a>');
?>
结果:
Test paragraph. Other text
<p>Test paragraph.</p> <a href="#fragment">Other text</a>
答案 1 :(得分:0)
可能更简单,但考虑到要求并且不知道字符串中的任何变化:
$new = preg_replace('/<a href=[^>]+>/', '', str_replace('</a>', '', $string));