views:

90

answers:

5

Hey,

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?

Thanks!

+1  A: 

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

BarsMonster
-1. What kind of answer is this?
Alin Purcaru
@Alin It's not *that* bad an answer, I'd say -- if the client is already using characters like ç and ®, then using UTF-8, say, for the input, storage and output of the CMS will enable you to do away with the need for the HTML entities. Just like it's doing on this web page, for example, where I just typed in "®", sent it to the site, and the site's displaying it as "®" -- look in the source code for this page, and you won't see an HTML entity there, just the actual, real character.
Matt Gibson
+1  A: 

You can use:

htmlentities

TheCandyMan666
Thank you! It works :)
Fernando Valente
+1  A: 

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 » 
Gordon
+5  A: 

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.

jmz
+1 for specifying the encoding.
Daniel Vandersluis
A: 

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.

borrible
Thank you! It works :)
Fernando Valente