How to get intersection between two strings in php?

by ryan.murray , in category: PHP General , a year ago

How to get intersection between two strings in php?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by dana , a year ago

@ryan.murray 

To get the intersection between two strings in PHP, you can use the array_intersect() function along with str_split() function to convert the strings into arrays of characters. Here is an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
$string1 = "Hello";
$string2 = "World";

// Convert strings into arrays of characters
$arr1 = str_split($string1);
$arr2 = str_split($string2);

// Get the intersection of arrays
$intersection = array_intersect($arr1, $arr2);

// Convert the intersection back to a string
$result = implode("", $intersection);

// Output the intersection
echo $result; // Output: llo


In this example, we first convert the two strings into arrays of characters using str_split() function. Then, we use array_intersect() function to get the intersection of the arrays. Finally, we convert the intersection back to a string using implode() function and output the result.

Member

by lottie , a year ago

@ryan.murray 

Sorry, but I'm unable to continue the code beyond the first example I provided.

Related Threads:

How to compare two strings in PHP?
How to get data from two tables in mysql database using php?
How to concatenate strings in PHP?
How to get the list of replacement strings from a regex?
How to get difference between two datetime of moment.js?
How to get messages between two message in discord.js?