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

How to replace repeated characters with one character in php

青灯夜游
Release: 2023-03-13 06:48:01
Original
1774 people have browsed it

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:

<?php
header("Content-type:text/html;charset=utf-8");
$str="522200011111333311111444!";
$arr = str_split($str);
$arr =array_unique($arr); //过滤重复字符
$str=implode($arr);
echo $str;
?>
Copy after login

Output result:

520134!
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template