How to find intersection of strings in php

青灯夜游
Release: 2023-03-16 17:44:01
Original
1700 people have browsed it

Implementation steps: 1. Use the str_split() function to convert both strings into character arrays. The syntax "str_split(string)" or "str_split(string,1)" will return two Character array; 2. Use the array_intersect() function to compare two character arrays to find the intersection. The syntax "array_intersect(character array 1, character array 2)" will return an intersection array containing the same character elements.

How to find intersection of strings in php

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

In php, you can use arrays and str_split () and array_intersect() functions to find the intersection of two strings.

Implementation steps:

Step 1: Use the str_split() function to convert both strings into character arrays

## The #str_split() function splits a string into an array.

str_split(string,length)
Copy after login

ParametersDescriptionRequired . Specifies the string to be split. Optional. Specifies the length of each array element. The default is 1.
string
length
str_split() function can split the string according to the specified length and pass it into the array. When the split length is 1, it can be converted into a character array.


<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str1 = &#39;012hello3456&#39;;
$str2 = &#39;24Bhello68&#39;;
$arr1 = str_split($str1);
$arr2 = str_split($str2);
echo "字符串1:".$str1;
var_dump($arr1);
echo "字符串2:".$str2;
var_dump($arr2);
?>
Copy after login

How to find intersection of strings in php

will get two character arrays.

Step 2: Use the array_intersect() function to compare two character arrays and obtain the intersection

array_intersect(): Compare arrays and return the intersection of the two arrays (only compare key value).

array_intersect(array1,array2,array3...);
Copy after login

ParametersDescriptionRequired . The first array to compare with other arrays. Required. The array to compare to the first array. Optional. Additional array to compare with the first array.
array1
array2
array3,...
The intersection array returned by this function contains all values ​​in array1 that also appear in all other parameter arrays.

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str1 = &#39;012hello3456&#39;;
$str2 = &#39;24Bhello68&#39;;
$arr1 = str_split($str1);
$arr2 = str_split($str2);
echo "两个字符数组:";
var_dump($arr1);
var_dump($arr2);
$result=array_intersect($arr1,$arr2);
echo "交集数组:";
var_dump($result);
?>
Copy after login

How to find intersection of strings in php

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of How to find intersection of strings 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