Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert special characters to HTML character codes

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?

like image 318
Fernando Valente Avatar asked Oct 07 '10 14:10

Fernando Valente


People also ask

Which function will convert special characters to HTML entities?

The htmlentities() function converts characters to HTML entities.

What's the difference between HTML entities () and htmlspecialchars ()?

Difference between htmlentities() and htmlspecialchars() function: The only difference between these function is that htmlspecialchars() function convert the special characters to HTML entities whereas htmlentities() function convert all applicable characters to HTML entities.

What is HTML special characters PHP?

The htmlspecialchars() function converts special characters into HTML entities. It is the in-built function of PHP, which converts all pre-defined characters to the HTML entities.


1 Answers

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.

like image 144
jmz Avatar answered Sep 22 '22 08:09

jmz