将特殊字符转换成HTML字符代码
问题描述:
我正在为客户开发CMS,他需要编辑内容并使用特殊字符,例如ç
和®
。但是,我不希望他输入字符代码,例如& reg;
。有没有人知道一个好的方法来自动转换这些字符使用PHP?
I'm developing a CMS for a customer and he needs to edit stuff and use special characters such as ç
and ®
. However, I don't want him to have to enter the character codes like ®
. Does anyone knows a good way to automatically convert those characters using PHP?
答
你可以使用htmlentities / p>
You can use htmlentities() to do that.
php -r 'echo htmlentities("®ç", ENT_COMPAT, "UTF-8"), "\n";'
®ç
要将实体回到可读文本,请使用html_entity_decode():
To turn entities back to readable text, use html_entity_decode():
php -r 'echo html_entity_decode("®ç", ENT_COMPAT, "UTF-8"), "\n";'
®ç
如果您不使用unicode,请省略字符集名称,正确的字符集。
If you're not using unicode, omit the charset name or give the correct charset.