Comparing two strings, one string has a wildcard
I have two lines that I would like to compare.
string1 = www.domain.com
string2 = *.domain.com
Usually, if we use if ($string1 == $string2)..
, they shouldn't be the same as they are not equal, since string2 is a wildcard, I'd like it to be a match.
Condition (s)
-
*.domain.com
must match***anything**.domain.com*
-
*.domain.com
must be a mismatch for*domain.com*
or*something.adifferent.com*
- need a solution to support all ie TLDs
.com, .net, .org. .com.au
etc.
How can I achieve this?
source to share
I'm not sure about the 2 points you are talking about in your question, but I think you need to do 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
Note: 1. If you want the following to match, also go to the preset 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,'.')){
source to share