I'm using imagettftext to create images of certain text characters. To do this, I need to convert an array of hexidecimal character codes to their HTML equivalents and I can't seem to find any functionality built-in to PHP to do this. What am I missing? (through searching, I came across this: PHP function imagettftext() and unicode, but it none of the answers seem to do what I need - some characters convert but most don't).
Here's the resulting HTML representation (in the browser)
[characters] => Array
(
[33] => A
[34] => B
[35] => C
[36] => D
[37] => E
[38] => F
[39] => G
[40] => H
[41] => I
[42] => J
[43] => K
[44] => L
)
Which comes from this array (not capable of rendering in imagettftext):
[characters] => Array
(
[33] => A
[34] => B
[35] => C
[36] => D
[37] => E
[38] => F
[39] => G
[40] => H
[41] => I
[42] => J
[43] => K
[44] => L
)
Based on a sample from the PHP manual, you could do this with a regex:
$newText = preg_replace('/&#x([a-f0-9]+)/mei', 'chr(0x\\1)', $oldText);
I'm not sure a raw html_entity_decode()
would work in your case, as your array elements are missing the trailing ;
-- a necessary part of these entities.
EDIT, July 2015:
In response to Ben's comment noting the /e
modifier being deprecated, here's how to write this using preg_replace_callback()
and an anonymous function:
$newText = preg_replace_callback(
'/&#x([a-f0-9]+)/mi',
function ($m) {
return chr(hexdec($m[1]));
},
$oldText
);