Home>Article>Backend Development> How to replace repeated characters with one character in php

How to replace repeated characters with one character in php

青灯夜游
青灯夜游 Original
2021-09-27 19:43:06 1685browse

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.

How to replace repeated characters with one character in php

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:

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!

Statement:
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