I have two strings which I would like to compare.
string1 = www.domain.com
string2 = *.domain.com
Normally, if we use if ($string1 == $string2).. these shouldn't match as they are not equal, however, since string2 is a wildcard, I would like it to be a match.
Condition(s)
*.domain.com should match with ***anything**.domain.com**.domain.com should be a mismatch for *domain.com* or *something.adifferent.com*.com, .net, .org. .com.au etc..How can I achieve this?
I am not sure about 2 point that you tell in your question, but i think you need do like this:-
<?php
$string1 = '*.domain.com';
$string2 = 'www.domains.com';
$pattern = 'domain'; // put any pattern that you want to macth in both the string.
if(preg_match("~\b$pattern\b~",$string1) && preg_match("~\b$pattern\b~",$string2) && substr_count($string1,'.') == substr_count($string2,'.')){
echo 'matched';
}else{
echo 'not matched';
}
?>
Output:- http://prntscr.com/7alzr0
and if string2 converted to www.domain.com then
http://prntscr.com/7am03m
Note:- 1. if you want the below will match also then go for the given condition.
example:--
$string1 = '*.domain.com';
$string2 = 'abc.login.domain.com';
if(preg_match("~\b$pattern\b~",$string1) && preg_match("~\b$pattern\b~",$string2) && substr_count($string1,'.') <= substr_count($string2,'.')){
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With