如何判断一个字母是否是 html 实体?

How to tell if a letter is a html entity?

我对 HTML Entities 感到困惑。

我正在从远程数据库获取数据。为了存储 blank space,数据团队使用 ISO-8859-1 Symbols。所以空白 </code> 被存储为 <code>&nbsp;.

现在,我正在查询此数据,并且由于某些其他原因,我需要测试返回的字符是普通字符还是 Html 实体。

我的解决方案是使用 htmlspecialchars(),像这样:

$ch = '&nbsp;';

if(htmlspecialchars(' ') == $ch)
    echo 'same';
else
    echo 'not same';

此代码将始终 returns not same!

编辑

阅读评论后,我将重新表述我的问题。

给定一个像这样的字符串 123&nbsp;456,在视觉上,它将被解释为 123 456。那么,我怎么知道那个空白是 Space 还是 Non-breaking space.

所以,我的问题是:如何获取空白 space 的 html 实体?

您可能希望使用 htmlentities 而不是 htmlspecialchars,因为这样可以匹配更多字符,尽管仍然无法匹配 space 字符,因为没有 html space.

的实体

如果你想匹配 non-breaking space,你可以这样做:

if('&nbsp;' == $ch)