Home  >  Article  >  Backend Development  >  How to remove multiple identical characters in php

How to remove multiple identical characters in php

青灯夜游
青灯夜游Original
2022-04-22 20:50:242638browse

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.

How to remove multiple identical characters in php

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(&#39;content-type:text/html;charset=utf-8&#39;);   
$str = "1.2.3.1.2.3.4.5.6.7.8.9";
$arr=str_split($str);
var_dump($arr);

?>

How to remove multiple identical characters in php

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);

How to remove multiple identical characters in php

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;

How to remove multiple identical characters in php

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!

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