比较两个字符串,其中一个字符串有一个通配符



我想比较两个字符串

string1 = www.domain.com

string2 = *.domain.com

通常,如果我们使用if ($string1 == $string2)..,它们不应该匹配,因为它们不相等,但是,由于string2是一个通配符,我希望它是一个匹配。

条件(s)

  1. *.domain.com应与***anything**.domain.com*匹配
  2. *.domain.com应该与*domain.com**something.adifferent.com*不匹配
  3. 需要一个解决方案来支持所有顶级域名,即.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,则

http://prntscr.com/7am03m

注意:- 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,'.')){ 

最新更新