我想比较两个字符串
string1 = www.domain.com
string2 = *.domain.com
通常,如果我们使用if ($string1 == $string2)..
,它们不应该匹配,因为它们不相等,但是,由于string2是一个通配符,我希望它是一个匹配。
条件(s)
-
*.domain.com
应与***anything**.domain.com*
匹配 -
*.domain.com
应该与*domain.com*
或*something.adifferent.com*
不匹配 - 需要一个解决方案来支持所有顶级域名,即
.com, .net, .org. .com.au
等。
我怎样才能做到这一点?
你的问题中有两点我不太确定,但我认为你需要这样做:-
<?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$patternb~",$string1) && preg_match("~b$patternb~",$string2) && substr_count($string1,'.') == substr_count($string2,'.')){
echo 'matched';
}else{
echo 'not matched';
}
?>
输出:http://prntscr.com/7alzr0
,如果string2
转换为www.domain.com
,则
注意:- 1。如果你想下面的也会匹配,然后去给定的条件。
example:--
$string1 = '*.domain.com';
$string2 = 'abc.login.domain.com';
if(preg_match("~b$patternb~",$string1) && preg_match("~b$patternb~",$string2) && substr_count($string1,'.') <= substr_count($string2,'.')){