Home > Article > Backend Development > How to determine whether it is Chinese or English in php
The way PHP determines whether it is Chinese or English is to use the preg_match() function, which can perform a regular expression matching. The specific usage method is: [preg_match("/^[^/x80-/xff] $/", $s)].
Function introduction:
(Recommended tutorial: php tutorial)
preg_match function is used Perform a regular expression match.
This function returns the number of matches of pattern. Its value will be 0 (no match) or 1 because preg_match() will stop searching after the first match. preg_match_all() differs from this in that it searches for the subject until it reaches the end. If an error occurs preg_match() returns FALSE.
Function syntax:
int preg_match(string $pattern, string $subject[, array &$matches[, int $flags = 0[, int $offset = 0]]])
Code implementation:
static function ischinese($s){ $allen = preg_match("/^[^/x80-/xff]+$/", $s); //判断是否是英文 $allcn = preg_match("/^[".chr(0xa1)."-".chr(0xff)."]+$/",$s); //判断是否是中文 if($allen){ return 'allen'; }else{ if($allcn){ return 'allcn'; }else{ return 'encn'; } } }
The above is the detailed content of How to determine whether it is Chinese or English in php. For more information, please follow other related articles on the PHP Chinese website!