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

How to determine whether it is Chinese or English in php

王林
Release: 2023-03-02 20:00:02
Original
6486 people have browsed it

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]]])
Copy after login

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';  
              }  
        }  
               
   }
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template