2

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?

502_Geek
  • 2,046
  • 2
  • 22
  • 32

2 Answers2

2

Try /\p{Han}+/gu like:

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

or

preg_match_all("/\p{Han}+/u", $message,$result);
Phil
  • 1,444
  • 2
  • 10
  • 21
  • I tried with `echo preg_match_all("/^\p{Han}+$/u", "01. 你好");` But, show 0 instead of 1. – 502_Geek Mar 14 '18 at 05:34
  • 1
    Please note the `^` should be removed since the Chinese characters are not at the beginning of the string. – Phil Mar 14 '18 at 05:49
1

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 "\n";
$message = "你好";
echo preg_match_all("/^\p{Han}+$/u", $message);
echo "\n";
$message = "01。你好";
echo preg_match_all("/^\p{Han}+$/u", $message);
echo "\n";
?>

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
Allan
  • 12,117
  • 3
  • 27
  • 51