Home  >  Article  >  Backend Development  >  How to use php to match Chinese characters and numbers

How to use php to match Chinese characters and numbers

墨辰丷
墨辰丷Original
2018-06-08 10:46:141203browse

This article mainly introduces the method of operating Chinese and number matching in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.

The specific implementation method is as follows:

function getChinese($string,$encode="GBK") {
  switch($encode){
   case "GBK" :$codelength=2;break;
   case "GB2312" :$codelength=3;break;
   case "UTF-8" :$codelength=3;break;
   case "UTF-16" :$codelength=4;break;
  }
  $tmpstr = '';
  $arr = array(1,2,3,4,5,6,7,8,9,0);
  $strlen = strlen($string);
  for($i=0; $i<$strlen; $i++) {
   $str=substr($string, $i, 1);
   $str1=trim($str);
   if( ord($str)>0xA0 ){
   $tmpstr.= substr($string, $i, $codelength);
   $i = $i+$codelength-1;
   }
  }
 return $tmpstr;
}

Summary: The above is the entire content of this article, I hope it can be helpful to everyone’s study .

Related recommendations:

The function of using curl to forge IP in PHP

php uses the MagickWand module to manipulate images Method of adding watermark

PHP simulates the method of response class in asp

The above is the detailed content of How to use php to match Chinese characters and numbers. 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