0

我正在使用 strip_tags() 函数来提取链接的内容(文本部分不是链接部分)。该字符串是从 twitter 获得的。不幸的是,它返回相同的东西。

代码如下

$a = $cur['source'];
echo strip_tags($a);

输入

<a href="http://twitter.com/download/android">Twitter for Android</a>

输出

<a href="http://twitter.com/download/android">Twitter for Android</a>

当我将输入复制粘贴到字符串并尝试 strip_tags 时,它工作得很好。可能是什么原因/?

4

1 回答 1

0

您需要使用html_entity_decode因为您的 HTML 看起来是 HTML,但实际上并非如此(事实上,所有 html 代码本身都被编码为其他 html 代码)。

如果你尝试echo htmlentities($cur['source']),你会看到

&lt;a href=&quot;http://twitter.com/download/android&quot;&gt;Twitter for Android&lt;/a&gt;

这意味着该字符串可以在浏览器上显示而不会被解释为 html(在您的情况下为链接)。例如,如果我想<html>在 Stackoverflow 上显示而不看到我的<html>标签被解释,这将非常有用。

于 2012-10-21T07:48:48.273 回答