Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php - regular expression to check if the string has chinese chars

I have the string $str and I want to check if it`s content has Chinese chars or not (true/false)

$str = "赕就可消垻,只有当所有方块都被消垻时才可以过关"; 

can you please help me?

Thanks! Adrian

like image 420
Adrian Avatar asked Feb 07 '11 15:02

Adrian


People also ask

How does PHP regex work?

In PHP, regular expressions are strings composed of delimiters, a pattern and optional modifiers. $exp = "/w3schools/i"; In the example above, / is the delimiter, w3schools is the pattern that is being searched for, and i is a modifier that makes the search case-insensitive.

Are Chinese characters Multibyte?

The chinese is a MULTIBYTE character and only UTF8 can handle this. and store the chinese characters in a NCLOB or NVARCHAR2 data types.

How do you call Chinese characters?

Chinese characters, 漢字 (simplified 汉字), are known by many names: “Sinograms” (from the Greek name of China), “Hànzì” (from Mandarin), “Hanja” (from Korean 한자), and “Kanji” (from Japanese かんじ). Whatever you prefer to call them, they are the most complex writing system in use today.

Does PHP support regex?

PHP has a built-in support for regular expressions too. In PHP, there are two modules for regular expressions: the POSIX Regex and the PCRE. The POSIX Regex is depreciated. In this chapter, we will use the PCRE examples.


2 Answers

You could use a unicode character class http://www.regular-expressions.info/unicode.html

preg_match("/\p{Han}+/u", $utf8_str); 

This just checks for the presence of at least one chinese character. You might want to expand on this if you want to match the complete string.

like image 59
mario Avatar answered Sep 20 '22 09:09

mario


@mario answer is right!

For Chinese chars use this regex: /[\x{4e00}-\x{9fa5}]+/u

And Don't forget the u modifier!!!

About u modifier reference

TKS to mario

like image 29
eaglewu Avatar answered Sep 20 '22 09:09

eaglewu