移除<a> tags via PHP &amp; keep content of tag?

时间:2018-04-25 14:43:51

标签: php tags string-matching

Is it possible to remove all <a> tags / links from strings while keeping the content? strip_tags is not an option, because there are <p> tags in it which I want to keep. The links are structured this way:

<a style="color: #000000" href="{url}">***content***</a>

1 个答案:

答案 0 :(得分:1)

strip_tags方法接受第二个可选参数,标签不能删除。

http://php.net/manual/en/function.strip-tags.php

来自Php.net的例子:

<?php $text = '<p>Test paragraph.</p><!-- Comment --> <a ref="#fragment">Other text</a>';
echo strip_tags($text);
echo "\n";
// Allow <p> and <a>
echo strip_tags($text, '<p><a>');
?>
The above example will output:
Test paragraph. Other text
<p>Test paragraph.</p> <a href="#fragment">Other text</a>