What encoding is this ... and how do you avoid it in php?
I'm working on an imdb data scraper for a site and I seem to be coding everything in a weird encoding that I haven't seen before.
<a href="/keyword/exploding-ship/">Exploding Ship</a>
A Bug's Life
Is there a php function that converts them to regular characters?
+2
user15063
source
to share
2 answers
These are SGML character escape sequences. They can be either decimal ( '
) or hex (  
) and refer directly to the Unicode code point.
html_entity_decode () should work in PHP 5. Although I can't check at the moment.
The first comment on this man page provides the following code for older PHP versions:
// For users prior to PHP 4.3.0 you may do this:
function unhtmlentities($string)
{
// replace numeric entities
$string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string);
$string = preg_replace('~&#([0-9]+);~e', 'chr("\\1")', $string);
// replace literal entities
$trans_tbl = get_html_translation_table(HTML_ENTITIES);
$trans_tbl = array_flip($trans_tbl);
return strtr($string, $trans_tbl);
}
+1
source to share