Php 使用 strip_tags 忽略 <a> 标签中的文本

Php using strip_tags to ignore text in <a> tags

我想去除标签。 当我使用

$ta=strip_tags($_REQUEST['textarea'],'<a>');

它 returns <a> 标签。 如果我使用

$ta=strip_tags($_REQUEST['textarea']);

它包括 <a href> 的内部。

我只想要文字。例如这个 html

$text= '<p>test paragraph.</p>'<a href="index.php">Click link</a>';

我只想要 test paragraph,但我得到 test paragraph.Click link

感谢您的帮助

如果只有 <a href 标签你不喜欢,正如上面评论中所评论的那样,这应该清除它们并留给你可以使用 strip_tags() 轻松删除的其余部分。

$text= '<p>test paragraph.</p><a href="index.php">Click link</a><p>test paragraph.</p><a href="index.php">Click link</a><p>test paragraph.</p>';

$pos = strpos($text, "<a href"); // find first a href

while($pos !== false){ // loop until there is no more a href
    $pos2 = strpos($text, "</a>", $pos)+4; // find the end tag of the a
    $text = substr($text, 0, $pos) . substr($text, $pos2); // remove the tag and link text
    $pos = strpos($text, "<a href"); // find the next. If none is found "false" is returned meaning while ends.
}

echo strip_tags($text); // strip away other tags.

https://3v4l.org/YtJic