Convert special characters to HTML character codes

我的未来我决定 提交于 2019-11-30 20:45:50

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.

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 » 

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

You can use:

htmlentities

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.

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