Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check unicode in PHP

How can I check whether a character is a Unicode character or not with PHP?

like image 702
Orion Avatar asked Aug 29 '09 07:08

Orion


People also ask

How do I check if a string is UTF-8 in PHP?

is_utf8() – check for UTF-8 With this PHP function it's possible to check whether a string is encoded as UTF-8 or not, or seems to be, at least. It scans a string for invalid UTF-8 characters (or bytes) and returns false, if it finds any.

Does PHP use Unicode?

PHP does not offer native Unicode support. PHP only supports a 256-character set. However, PHP provides the UTF-8 functions utf8_encode() and utf8_decode() to provide some basic Unicode functionality. See the PHP manual for strings for more details about PHP and Unicode.

How do I find Unicode?

To insert a Unicode character, type the character code, press ALT, and then press X. For example, to type a dollar symbol ($), type 0024, press ALT, and then press X.

How do I know if a string is encoded?

In PHP, mb_detect_encoding() is used to detect the character encoding. It can detect the character encoding for a string from an ordered list of candidates. This function is supported in PHP 4.0. 6 or higher version.


2 Answers

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];
like image 122
Alix Axel Avatar answered Oct 04 '22 03:10

Alix Axel


you can try with

mb_check_encoding($s,"UTF-8")

link

like image 35
Svetlozar Angelov Avatar answered Oct 04 '22 04:10

Svetlozar Angelov