doukoumi3389 2015-05-29 01:44
浏览 95
已采纳

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

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)

  1. *.domain.com should match with ***anything**.domain.com*
  2. *.domain.com should be a mismatch for *domain.com* or *something.adifferent.com*
  3. need a solution to support all TLD's i.e. .com, .net, .org. .com.au etc..

How can I achieve this?

  • 写回答

1条回答 默认 最新

  • douyinmian8151 2015-05-29 02:16
    关注

    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,'.')){ 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器