Home > Article > Backend Development > How to remove duplicate characters from php string
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.
Recommended: "PHP Video Tutorial"
php removes repeated characters from a string
<?php $str = '蚂蚁蚂蚁学院学院,我非常爱爱爱爱爱你!522200011111333311111444'; 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('',$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!