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.

Comment Icon

0 Comments

There are no comments yet. Why not be the first?

Add new comment

Filtered HTML

  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <pre> <ul> <ol> <li> <dl> <dt> <dd> <img> <p>
  • You can use BBCode tags in the text. URLs will automatically be converted to links.
  • You can enable syntax highlighting of source code with the following tags: <code>, <pre>, <bash>, <css>, <html>, <js>, <jquery>, <mysql>, <php>. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.