Home  >  Article  >  Backend Development  >  How to remove repeated letters in string in php

How to remove repeated letters in string in php

青灯夜游
青灯夜游Original
2021-05-27 16:20:092143browse

php method to remove duplicate letters: first use the str_split() function to convert the string into a character array; then use the array_unique() function to remove duplicate values ​​in the array; finally use implode() to convert the processed Just convert the array into a string.

How to remove repeated letters in string in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php removes duplicates in the string Letters

<?php
$str = &#39;abacade&#39;;
$array = str_split($str);
$str = implode(array_unique($array));
var_dump($str);
?>

Output:

How to remove repeated letters in string in php

## Related function description:

str_split() function splits a string into an array. The syntax is as follows:

str_split(string,length)

##Parameterstringlength
Description
Required. Specifies the string to be split.
Optional. Specifies the length of each array element. The default is 1.
array_unique()

Function is used to remove duplicate values ​​from an array. If two or more array values ​​are the same, only the first value is retained and the other values ​​are removed. Note: The retained array will retain the key type of the first array item.

Syntax

array_unique(array)

Parametersarraysortingtypeimplode() function returns a string composed of array elements. The syntax is as follows:
Description
Required. Specifies an array.
Optional. Specifies the sorting type. Possible values: SORT_STRING - Default. Treat each item as a string.
  • SORT_REGULAR - Sort each item in regular order (Standard ASCII, does not change type).
  • SORT_NUMERIC - Treat each item as a number.
  • SORT_LOCALE_STRING - Treat each item as a string, based on the current locale (can be changed via setlocale()).
implode(separator,array)

##ParameterDescriptionseparatorarray Recommended study: "PHP Video Tutorial
Optional. Specifies what is placed between array elements. Default is "" (empty string).
Required. Arrays to be combined into strings.
"

The above is the detailed content of How to remove repeated letters in string 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