4

I have a file which has many html entities. I need to convert html entities to hex entities.

Example: & to &

Is there any function for the conversion of html to hex entities? If no, which way would be the effective and fastest way to achieve this?

Learning
  • 848
  • 1
  • 9
  • 32

1 Answers1

0

First of all, the "hex entities" are the entities with the character represented as Unicode codepoint. All Unicode characters could be represented as entities with the Unicode codepoint; in HTML, some can be represented with just a name, instead.

The list of entities in HTML which have a predefined name is quite long: http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references#Character_entity_references_in_HTML

If you have a text which in which HTML entities are already converted using the shorthand name, then your only option is to do a search and replace. Needless to say, that can be quite computationally intense. The code would look like:

<?php
$str = 'Hello &amp; world! &quot;';

$find = ['&amp;', '&quot;']; //.. Complete the table with the entire list
$replace = ['&#x00026;', '&#x00022;']; // ... Complete this list too
$str = str_replace($find, $replace, $str);
echo $str;
?>

However, this can be very slow.

ItalyPaleAle
  • 7,185
  • 6
  • 42
  • 69