Method: 1. Use the str_split() function to split the string and convert the string into a character array. One character in the string corresponds to an element in the array; 2. Use the array_unique() function to filter the array. of repeated characters; 3. Use the implode() function to convert the deduplicated array into a string.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php replace the repeated characters with One character
Implementation idea: Use the array method array_unique()
Use str_split() to split the string and convert the string into a character array ;
Use array_unique() to filter duplicate characters
Use implode() to convert the array into a string
Implementation code:
<?php header("Content-type:text/html;charset=utf-8"); $str="522200011111333311111444!"; $arr = str_split($str); $arr =array_unique($arr); //过滤重复字符 $str=implode($arr); echo $str; ?>
Output result:
520134!
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to replace repeated characters with one character in php. For more information, please follow other related articles on the PHP Chinese website!