Home >Backend Development >PHP Tutorial >PHP uses custom functions to implement Chinese character segmentation and replacement function examples

PHP uses custom functions to implement Chinese character segmentation and replacement function examples

高洛峰
高洛峰Original
2017-02-03 15:22:021451browse

The example of this article describes the PHP custom function to implement the Chinese character segmentation and replacement function. Share it with everyone for your reference, the details are as follows:

header("Content-type:text/html;charset=utf-8");
$str="赵钱孙";
function mbstringToArray($str,$charset) {
  $strlen=mb_strlen($str);
  while($strlen){
    $array[]=mb_substr($str,0,1,$charset);
    $str=mb_substr($str,1,$strlen,$charset);
    $strlen=mb_strlen($str);
  }
  return $array;
}
//用法gbk utf-8
$arr = mbstringToArray($str,"utf-8");
var_dump($arr);

The running results are as follows:

array(3) {
 [0]=>
 string(2) "赵"
 [1]=>
 string(2) "钱"
 [2]=>
 string(2) "孙"
}

Hope this article It will be helpful to everyone in PHP programming.

For more relevant articles on PHP using custom functions to implement Chinese character segmentation and replacement, please pay attention to 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