Home >Backend Development >PHP Tutorial >PHP implements filtering Chinese and numeric examples in strings_php skills

PHP implements filtering Chinese and numeric examples in strings_php skills

WBOY
WBOYOriginal
2016-05-16 20:09:531027browse

The example in this article describes the PHP implementation of filtering Chinese characters and numbers in strings. Share it with everyone for your reference. 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;
}

I hope this article will be helpful to everyone’s PHP programming design.

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