Home  >  Article  >  Backend Development  >  How to determine whether it is Chinese or English in php

How to determine whether it is Chinese or English in php

王林
王林Original
2020-07-21 16:30:446449browse

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)].

How to determine whether it is Chinese or English in php

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn