Home > Article > Backend Development > How to remove multiple identical characters in php
Removal method: 1. Use "str_split($str)" to convert the string into a character array, one character corresponds to an array element; 2. Use "array_unique($arr)" to remove the same characters in the character array Characters; 3. Use "implode("",$newArr)" to convert the deduplicated array into a string.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php remove multiple Same characters
In PHP, you can use the array_unique() function of the array to remove multiple identical characters from the string.
Implementation steps:
1. Use the str_split() function to convert the string into a character array. One character is an array element
<?php header('content-type:text/html;charset=utf-8'); $str = "1.2.3.1.2.3.4.5.6.7.8.9"; $arr=str_split($str); var_dump($arr); ?>
You can see that there are many repeated character elements
2. Use the array_unique() function to deduplicate the array
$newArr=array_unique($arr); var_dump($newArr);
ok, multiple identical characters have been removed
3. Use the implode() function to convert the deduplicated array into a string
The connector connecting each element of the array needs to be set to an empty character
$newStr=implode("",$newArr); echo $newStr;
You’re done!
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to remove multiple identical characters in php. For more information, please follow other related articles on the PHP Chinese website!