Is there a function that will change UTF-8 to Unicode leaving non special characters as normal letters and numbers?
ie the German word "tchüß" would be rendered as something like "tch\20AC\21AC" (please note that I am making the Unicode codes up).
EDIT: I am experimenting with the following function, but although this one works well with ASCII 32-127, it seems to fail for double byte chars:
function strToHex ($string)
{
$hex = '';
for ($i = 0; $i < mb_strlen ($string, "utf-8"); $i++)
{
$id = ord (mb_substr ($string, $i, 1, "utf-8"));
$hex .= ($id <= 128) ? mb_substr ($string, $i, 1, "utf-8") : "&#" . $id . ";";
}
return ($hex);
}
Any ideas?
EDIT 2: Found solution: The PHP ord() function does not work for double byte chars. Use instead: http://nl.php.net/manual/en/function.ord.php#78032
I guess you're going to print out your strings on a website?
I'm storing all my databases in uft8, using html_entities($string) before output.
Maybe you have to try html_entities(utf8_encode($string));
With PHP 7, there is a new IntlChar::ord() to find the Unicode Code Point from a given UTF-8 character:
I once created a function called _convert() which encodes safely everything to UTF-8.
Tested on php 5.6
Converting one character set to another can be done with iconv:
http://php.net/manual/en/function.iconv.php
Note that UTF is already an Unicode encoding.
Another way is simply using htmlentities with the right character set:
http://php.net/manual/en/function.htmlentities.php
For people looking to find the Unicode Code Point for any character this might be useful. You can then encode the string in whatever you want, replacing certain characters with escape codes, and leaving others in their binary form (eg. ascii printable characters), depending on the context in which you want to use it.
From: Mapping codepoints to Unicode encoding forms