Home  >  Article  >  Backend Development  >  How to remove duplicate characters from php string

How to remove duplicate characters from php string

藏色散人
藏色散人Original
2020-08-22 10:26:315020browse

How to remove duplicate characters from a php string: first split the string through the "mbstringtoarray" method; then use the "array_unique" function to filter duplicate characters; and finally merge the arrays through the "implode" method.

How to remove duplicate characters from php string

Recommended: "PHP Video Tutorial"

php removes repeated characters from a string

<?php
$str = &#39;蚂蚁蚂蚁学院学院,我非常爱爱爱爱爱你!522200011111333311111444&#39;;
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;
}
$arr = mbstringtoarray($str,"gbk"); //分割字符串
$arr =array_unique($arr); //过滤重复字符
$str = implode(&#39;&#39;,$arr); //合并数组
echo $str;
?>

Execution result:

蚂蚁学院,我非常爱你!520134

The above is the detailed content of How to remove duplicate characters from php string. 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