Hi,
I'm having a lot of trouble with unicode (UTF-16) values and PHP/XML. I want to read a set of unicode values from XML and output the correct glyphs to the browser. I've tried with UTF-8 and I get the same problem.
This is a simple working example I used for my first test:
$text = "\x00\x41";
$text = mb_convert_encoding($text, "ASCII", "UTF-16");
echo $text;
Output of above code:
A
However, when I try to get the values from XML things stop working.
XML:
<glyphs>
<code>0041</code>
<code>0042</code>
<code>0043</code>
<code>0044</code>
<code>0045</code>
<code>0046</code>
</glyphs>
In php I read each value from the above xml, split into pairs and format, e.g. \x00\x41, etc.
PHP:
// load xml
$xml = simplexml_load_file('encoding.xml');
if ($xml) {
// get families
foreach($xml->children() as $item) {
$pairs = str_split($item, 2);
$hex = "\x" . $pairs[0] . "\x" . $pairs[1];
// check value...
echo $hex . '<br/>';
$text = mb_convert_encoding($hex, "ASCII", "UTF-16");
echo $text;
}
}
else {
return 'The input is malformed.';
}
Output in browser:
\x00\x41
????
\x00\x42
????
\x00\x43
????
\x00\x44
????
\x00\x45
????
\x00\x46
????
Question marks should be A, B, C, D, E, F.
What am I doing wrong?
Thanks.