Convert special characters to HTML character codes

自古美人都是妖i 提交于 2019-12-30 06:39:14

问题


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?


回答1:


You can use htmlentities() to do that.

php -r 'echo htmlentities("®ç", ENT_COMPAT, "UTF-8"), "\n";'
®ç

To turn entities back to readable text, use html_entity_decode():

php -r 'echo html_entity_decode("®ç", ENT_COMPAT, "UTF-8"), "\n";'
®ç

If you're not using unicode, omit the charset name or give the correct charset.




回答2:


The easiest would be to use UTF-8 right from the start.
But you can also automatically convert characters with DOM:

$dom = new DOMDocument;
$dom->appendChild(new DOMText('© oui içi » '));
echo $dom->saveHtml();

outputs

© oui içi » 



回答3:


Use unicode, and show him how to copy&paste from character map :-)




回答4:


You can use:

htmlentities




回答5:


Take a look at the htmlentities function. This takes a string and converts component characters into their HTML entities. You can specify the encoding of the string to be consistent with the user's input.



来源:https://stackoverflow.com/questions/3882567/convert-special-characters-to-html-character-codes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!