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.
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)
Description | |
---|---|
string | Required . Specifies the string to be split.|
length | Optional. Specifies the length of each array element. The default is 1.
<?php header('content-type:text/html;charset=utf-8'); $str1 = '012hello3456'; $str2 = '24Bhello68'; $arr1 = str_split($str1); $arr2 = str_split($str2); echo "字符串1:".$str1; var_dump($arr1); echo "字符串2:".$str2; var_dump($arr2); ?>
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...);
Description | |
---|---|
array1 | Required . The first array to compare with other arrays.|
array2 | Required. The array to compare to the first array.|
array3,... | Optional. Additional array to compare with the first array.
<?php header('content-type:text/html;charset=utf-8'); $str1 = '012hello3456'; $str2 = '24Bhello68'; $arr1 = str_split($str1); $arr2 = str_split($str2); echo "两个字符数组:"; var_dump($arr1); var_dump($arr2); $result=array_intersect($arr1,$arr2); echo "交集数组:"; var_dump($result); ?>
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!