Skip to content

Instantly share code, notes, and snippets.

@davidkryzaniak
Created October 18, 2020 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidkryzaniak/90698193021cca3d57c325fbbaf00d9e to your computer and use it in GitHub Desktop.
Save davidkryzaniak/90698193021cca3d57c325fbbaf00d9e to your computer and use it in GitHub Desktop.
utf_hex_html_entities_decode
<?php
/**
* Converts a string like "The &#x00740069;me is:" to "The time is:"
*
* @param string $input Your string that might have UTF-8 characters that are html encoded.
* @return string A cleaned string
*/
function utf_hex_html_entities_decode($input)
{
return preg_replace_callback("/&#x([0-9a-fA-F]{8});/", function($m){return hex2bin($m[1]);}, $input);
}
echo utf_hex_html_entities_decode("&#x0066006b;\n"); //"fk"
echo utf_hex_html_entities_decode("&#x0066006C;owers\n"); //"flowers"
echo utf_hex_html_entities_decode("af&#x00660069;liate\n"); //"affiliate"
echo utf_hex_html_entities_decode("&#x00740069;me is\n"); //"time"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment