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

高洛峰
Release: 2023-03-05 11:02:01
Original
1436 people have browsed it

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

The running results are as follows:

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

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!

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