Unicode Character in PHP String

Unicode character in PHP string

Because JSON directly supports the \uxxxx syntax the first thing that comes into my mind is:

$unicodeChar = '\u1000';
echo json_decode('"'.$unicodeChar.'"');

Another option would be to use mb_convert_encoding()

echo mb_convert_encoding('က', 'UTF-8', 'HTML-ENTITIES');

or make use of the direct mapping between UTF-16BE (big endian) and the Unicode codepoint:

echo mb_convert_encoding("\x10\x00", 'UTF-8', 'UTF-16BE');

Check unicode in PHP

Actually you don't even need the mb_string extension:

if (strlen($string) != strlen(utf8_decode($string)))
{
echo 'is unicode';
}

And to find the code point of a given character:

$ord = unpack('N', mb_convert_encoding($string, 'UCS-4BE', 'UTF-8'));

echo $ord[1];

convert any string or a character in php to unicode code points

Use this function

function Unicode_decode($text) {
return implode(unpack('H*', iconv("UTF-8", "UCS-4BE", $text)));
}

If you want to have U+0000 use this :

for ($i=0; $i < strlen($word); $i++)
{
$wordConvert = Unicode_decode($word[$i]);
$result .= "U+" . substr($wordConvert, -4, 4) . "<br/>";

}

echo $result;

Print Unicode characters PHP

It looks like you have UTF-8 encoded strings internally, PHP outputs them properly, but your browser fails to auto-detect the encoding (it decides for ISO 8859-1 or some other encoding).

The best way is to tell the browser that UTF-8 is being used by sending the corresponding HTTP header:

header("content-type: text/html; charset=UTF-8");  

Then, you can leave the rest of your code as-is and don't have to html-encode entities or create other mess.

If you want, you can additionally declare the encoding in the generated HTML by using the <meta> tag:

  • <meta http-equiv=Content-Type content="text/html; charset=UTF-8"> for HTML <=4.01
  • <meta charset="UTF-8"> for HTML5

HTTP header has priority over the <meta> tag, but the latter may be useful if the HTML is saved to HD and then read locally.

What is the best way to replace unicode characters in a php string?

After following the lead from https://stackoverflow.com/users/395384/epb I used json_decode to translate the unicode which works.

$unicode = json_decode("\u00fc\u00be\u008c\u00a3\u00a4\u00bc") ;
$column = str_replace($unicode, "'", urldecode($row[$columnIndex]));

Why does php strpos() sometimes does not work on Unicode character?

Use:

$foo = hebrev($product->get_name()); 
$bar = hebrev("סט");
strpos($foo,$bar);

convert decimal value to unicode characters in php

While eval is generally to be avoided, it seems strictly-controlled enough to be fine here.

echo eval(sprintf('return "\u{%x}";',$val));


Related Topics



Leave a reply



Submit