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

How to remove duplicate characters from php string

藏色散人
Release: 2023-03-05 06:06:01
Original
5053 people have browsed it

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

Execution result:

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

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!

Related labels:
php
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