Entity Encode Text Using PHP

PHP Logo

Ever needed to Entity Encode a block of text (maybe to partially protect it from spam bots or just hide/obfuscate the content?). I did. It's pretty easy.

If it's just based Entity Encoding, you can always use htmlentities - however this only does the "important" characters such as quotes, ampersands and angle brackets.

If you want to entity encode ALL characters from a string, try this:

$phone_number = mb_encode_numericentity('0123 456789', array(0x00, 0xff, 0,  0xffff), 'UTF-8');

That produces:

0123 456789

This is pretty useful if you want to hide a phone number from things like the Skype auto-highlighter.