This question already has an answer here:
I tried to find out preg_match for Chinese character. I can get Chinese character in some way like this
preg_match("/^\p{Han}+/u", $message); //$message = '你好';
But, sometime two digit number with . in front something like this
$message = '01. 你好' //or '21. 你好'
So, my preg_match condition is not correct in this message. How do I check this optional two digit with dot?
</div>
Try /\p{Han}+/gu
like:
preg_match("/\p{Han}+/u", $message,$result);
or
preg_match_all("/\p{Han}+/u", $message,$result);
Could you have a look at the following php
prototype codes for matching and extracting only Chinese characters? Let me know if it works for you as expected.
$ more test.php test2.php
::::::::::::::
test.php
::::::::::::::
<?php
$string = 'abc 123 車飛行 abc 5344';
$pattern = '/[^\p{Han}]/u';
$replacement = '';
echo preg_replace($pattern, $replacement, $string);
?>
::::::::::::::
test2.php
::::::::::::::
<?php
$message = "01. 你好";
echo preg_match_all("/^\p{Han}+$/u", $message);
echo "
";
$message = "你好";
echo preg_match_all("/^\p{Han}+$/u", $message);
echo "
";
$message = "01。你好";
echo preg_match_all("/^\p{Han}+$/u", $message);
echo "
";
?>
The output of both codes are:
1) In order to extract only Chinese chars from the String.
$ php test.php
車飛行
2) In order to validate that the string does contain only Chinese chars.
$ php test2.php
0
1
0