利用php刪除字符串中重復(fù)字符。
比如:$str = "aaadddd"
最后得到的應(yīng)該是"ad"。
思路:
遍歷整個(gè)數(shù)組,然后依次取出第i個(gè)字符,存入一個(gè)臨時(shí)數(shù)組中,同時(shí)遍歷臨時(shí)數(shù)組,查找該數(shù)組中是否已經(jīng)存在該字符。
相關(guān)學(xué)習(xí)視頻推薦:php視頻教程
代碼如下:
$b = 'aaaaaaaaaaaaddddddttttttffff'; $tmp = array(); for($i = 0; $i < strlen($b); $i++){ $len = count($tmp); for($j = 0; $j < $len; $j++){ if($b[$i] === $tmp[$j]){ break; } } if($j == $len){ //如果最后的$j和$len相等的話,則表示$tmp臨時(shí)數(shù)組中不存在$b[$i] $tmp[] = $b[$i]; } } $result = implode($tmp); //輸出adtf
相關(guān)文章教程推薦:php基礎(chǔ)教程