php去掉重復字母的方法:首先使用str_split()函數將字符串轉換為字符數組;然后使用array_unique()函數移除數組中重復的值;最后使用implode()將處理后的數組轉換為字符串即可。
本教程操作環境:windows7系統、PHP7.1版,DELL G3電腦
php去掉字符串中重復的字母
<?php $str = 'abacade'; $array = str_split($str); $str = implode(array_unique($array)); var_dump($str); ?>
輸出:
相關函數說明:
str_split()
函數把字符串分割到數組中。語法如下:
str_split(string,length)
參數 | 描述 |
---|---|
string | 必需。規定要分割的字符串。 |
length | 可選。規定每個數組元素的長度。默認是 1。 |
array_unique() 函數用于移除數組中重復的值。如果兩個或