Reputation: 705
I'm still working on my library database.
With PHP, how can I compare strings with multiple words in a different order, like Gaius Julius Caesar
and Caesar Gaius Julius
to see if they match? Commas, dashes, double spaces etc. will be removed before the comparison.
To place the strings into two arrays and to check with in_array()
if every element of the first one is comprised in the second one could be a reasonable solution for a single couple of strings, but it seems a waste of CPU to check a list of over 5,000 names from a MariaDB table (5,000 × 5,000 = 25,000,000 of comparisons).
Suggestions?
Upvotes: 0
Views: 118
Reputation: 9440
Do it like this:
$a = "Gaius Julius Caesar";
$b = "Caesar Gaius Julius Putin";
$ar1 = explode(' ', $a);
$ar2 = explode(' ', $b);
$ar3 = array_intersect($ar1, $ar2);
print_r($ar3);
Yields
Array ( [0] => Gaius [1] => Julius [2] => Caesar )
Too see if they match - just compare their count()
Upvotes: 1