问题
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