I'm trying to see whether or not 2 strings match.
example: 1234.5678.9012.3456 => 5678.1234.3456 = match
This matches because the second string of number are also in the first one. I did this with the following code:
<?php
$haystack = '1234.5678.9012.3456';
$needle = '5678.1234.3456';
$regex = '/(?=.*'. str_replace(".",")(?=.*",$needle) .').*$/';
// regex looks like this /(?=.*5678)(?=.*1234)(?=.*3456).*$/
if(preg_match($regex, $haystack)){
echo "A match was found.";
} else {
echo "A match was not found.";
}
?>
Here is my problem, when there are duplicate numbers.
example: 1234.5678.9012.3456 => 5678.5678.3456 = dont match
1234.5678.5678.3456 => 5678.5678.3456 = match
The first example doesnt match because 5678 occurs twice but the first string only has 5678 once. In the second example 5678 occurs twice as well and therefor matches the second string.
My question: How could I change my regex,
You choose a very complicated way to do that. You can check what you want in a more simple way using array_diff
:
var_dump(array_diff(explode('.', $needle), explode('.', $haystack)));
when the resulting array is empty the condition is true.