Reputation: 1689
I'm trying to output the unicode for a character as a the return type for my PHP function, but when I call the function in practice it just outputs the code without the "0x" rather than a symbol. However, if I explicitly state the unicode in my HTML, it outputs the symbol fine. Below is a simplified version of my code. Why is this happening?
In a PHP file displaying a table:
<td><?php echo verifyMatch($a,$b) ?></td>
In my function in another file:
function verifyMatch($_a,$_b){
$_output = null;
if (checkCondition()){
$_output = 0x2714;
// unicode for tick
} else {
$_output = 0x2718;
// unicode for cross
}
return $_output;
}
Upvotes: 0
Views: 440
Reputation: 38436
As far as PHP is concerned, your values 0x2714
and 0x2718
are simply hexadecimal numbers and they are stored as just 2714
and 2718
, respectively. In actuality, PHP should be converting them to their decimal values instead. When outputted into HTML, they are being outputted as just that - numbers.
If you want to output them in HTML and have the actual symbols appear, try pre-pending them with &#x
and appending them with a ;
:
<td><?php echo '&#x' . verifyMatch($a, $b) . ';'; ?></td>
If they are being converted to their decimal values, you can prepend them with just &#
instead of &#x
. The added x
is for the hex-values.
Upvotes: 2
Reputation: 686
Ref: php chr with unicode values
function replace_unicode_escape_sequence($match) {
return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');
}
function unicode_chr ($chr) {
$str = "\u".end(explode("+", $chr));
return preg_replace_callback('/\\\\u([0-9a-f]{4})/i', 'replace_unicode_escape_sequence', $str);
}
function verifyMatch($_a,$_b){
$_output = null;
if (checkCondition()){
$_output = unicode_chr("U+2714"); // unicode for tick
}
else {
$_output = unicode_chr("U+2718"); // unicode for cross
}
return $_output;
}
Upvotes: 0