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 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助