Home >Backend Development >PHP Problem >How to remove identical items from a string in php

How to remove identical items from a string in php

青灯夜游
青灯夜游Original
2022-12-15 16:12:504546browse

Removal method: 1. Use str_split() to convert the string into a character array, the syntax is "str_split(string)"; 2. Use array_unique() or array_flip() to remove the same items of the array, the syntax is " array_unique(character array)" or "array_flip(array_flip(character array))"; 3. Use implode() to convert the deduplicated array into a string, the syntax is "implode("", deduplicated array)".

How to remove identical items from a string in php

The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer

In PHP, you want to remove strings Identical items (repeating characters) can use arrays.

Implementation idea:

  • Convert the string into a character array

  • Remove the character array The same items

  • Convert the deduplicated array to a string

Method 1: str_split() array_unique() implode ()

  • Use the str_split() function to convert the string into a character array. One character is an array element

  • Use array_unique () function to deduplicate the array

  • Use the implode() function to convert the deduplicated array into a string

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str = "1.2.3.1.2.3.4";
echo "原字符串:".$str."<br><br>";

$arr=str_split($str);
echo "字符数组:";
var_dump($arr);

$newArr=array_unique($arr);
echo "去重后的数组:";
var_dump($newArr);
$newStr=implode("",$newArr);
echo "去重后的字符串:".$newStr;
?>

How to remove identical items from a string in php

Method 2: str_split() array_flip() implode()

  • Use the str_split() function to convert the string into characters Array, one character is an array element

  • Use the array_flip() function twice to deduplicate the array

    array_flip is a function that reverses the keys and values ​​of the array. It has A feature is that if two values ​​in the array are the same, then the last key and value will be retained after inversion. Using this feature, we use it to indirectly implement deduplication of the array

  • Use the implode() function to convert the deduplicated array into a string

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str = "12hello13";
echo "原字符串:".$str."<br><br>";

$arr=str_split($str);
echo "字符数组:";
var_dump($arr);

$newArr=array_flip(array_flip($arr));
echo "去重后的数组:";
var_dump($newArr);
$newStr=implode("",$newArr);
echo "去重后的字符串:".$newStr;
?>

How to remove identical items from a string in php

Recommended learning: "PHP Video tutorial

The above is the detailed content of How to remove identical items from a 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