Home > Backend Development > PHP Problem > How to determine whether a string is repeated in php

How to determine whether a string is repeated in php

青灯夜游
Release: 2023-03-16 14:04:02
Original
2771 people have browsed it

Judgment steps: 1. Use str_split() to convert the string into a character array, the syntax "str_split (string)"; 2. Use array_unique() to remove duplicate values ​​​​in the character array, the syntax "array_unique( Character array)" will return a deduplication array; 3. Use count() to obtain the length of the character array and the deduplication array; 4. Determine whether the lengths of the two arrays are equal, the syntax is "Character array length == deduplication array length" , if equal, the strings are not repeated.

How to determine whether a string is repeated in php

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

In php, you can use arrays To determine whether the string is repeated.

Judgment steps:

Step 1. Use the str_split() function to convert the string into a character array

## The #str_split() function splits a string into characters, and these characters form an array.

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str="Helloele";
var_dump($str);
$arr=str_split($str);
var_dump($arr);
?>
Copy after login

How to determine whether a string is repeated in php

Step 2: Use array_unique() to remove duplicate values ​​in the character array and return the deduplicated array

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.

$res=array_unique($arr);
var_dump($res);
Copy after login

How to determine whether a string is repeated in php

Step 3: Use the count() function to get the length of the character array and deduplication array

The count() function can Count the number of all elements in the array, that is, get the array length.

$len1=count($arr);
$len2=count($res);
Copy after login

Step 4: Determine whether the lengths of the two arrays are equal

$len1==$len2
Copy after login

  • If the lengths of the two arrays are equal, there are no duplicate values ​​in the arrays

  • If the lengths of the two arrays are not equal, it means that an element has been removed, that is, there are duplicate values ​​in the array.

Complete code:

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str="Helloele";
var_dump($str);
$arr=str_split($str);
var_dump($arr);

$res=array_unique($arr);
var_dump($res);

$len1=count($arr);
$len2=count($res);
if($len1==$len2){
	echo "字符串不重复";
}else{
	echo "字符串有重复";
}

?>
Copy after login

How to determine whether a string is repeated in php## Recommended learning: "

PHP Video Tutorial

The above is the detailed content of How to determine whether a string is repeated 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