【问题标题】:Show error when url isnt allowed list?当 url 不是允许列表时显示错误?
【发布时间】:2014-10-15 14:08:38
【问题描述】:

如果 $text 不允许列表打印错误。不知道怎么弄

$allowedDomains = array('www.test1.com', 'www.test2.co.uk', 'test3.com');
$text = 'this is my url www.abcd.com/index.php?page=home';

【问题讨论】:

  • 使用 in_array 函数在数组中搜索。首先从 $text 字符串中获取 url。
  • 您是否要测试 $text 是否包含 $allowedDomains 值?你的问题是不是有点清楚?
  • 致 bhushya - 是的,这就是我想要的。

标签: php regex list url


【解决方案1】:

这样试试

$allowedDomains = array('www.test1.com', 'www.test2.co.uk', 'test3.com');

if ( !in_array('your-domain-name', $allowedDomains) {
    echo 'show your error here';
} else {
    echo  'do what ever you want here';
}

【讨论】:

  • 如果域名在文本内怎么办
【解决方案2】:

这是另一种方法:使用域列表创建一个正则表达式,然后使用它来测试您的文本字符串。

$allowedDomains = array('www.test1.com', 'www.test2.co.uk', 'test3.com');
# concatenate the allowed domains with | and surround with brackets so the regex will
# match any of the array items
# i.e. $regex is (www.test1.com|www.test2.co.uk|test3.com)
$regex = "(" . implode("|", $allowedDomains) . ")";

# a couple of test strings
$texts = array('this is my url www.abcd.com/index.php?page=home',
         'my home page is www.test3.com');

foreach ($texts as $t) {
    if (preg_match("/$regex/", $t)) {
        echo "Found a match! $t\n";
    }
    else {
        echo "No match found: $t\n";
    }
}

输出:

No match found: this is my url www.abcd.com/index.php?page=home

Found a match! my home page is www.test3.com

【讨论】:

    【解决方案3】:

    考虑到并非每个人都输入“http(s)”和/或“www”,我为您编写了一个函数,允许您随意添加/删除域名和/或域扩展名进入他们的域搜索,以及域后面可能包含哪些信息的不可预测性:

    <?php
    
    function testDomain($domain){
    
        //Set domain names
        $arrDomainNames = array("test1","test2","test3");
    
        //Set domain extensions like .com .co .info
        //Do NOT add .co.uk! .uk is taken care of in the regex
        $arrDomainExt = array("com","co");
    
        $cntDomainNames = count($arrDomainNames);
        $cntDomainExt = count($arrDomainExt);
        $i = 0;
        $y = 0;
    
        $DomNames = "";
        $DomExt = "";
    
        foreach($arrDomainNames as $value){
            if($i == $cntDomainNames - 1){
                $DomNames .= $value;
            } else {
                $DomNames .= $value ."|";
            }
            $i++;
        }
    
        unset($value);
    
        foreach($arrDomainExt as $value){
            if($y == $cntDomainExt - 1){
                $DomExt .= $value;
            } else {
                $DomExt .= $value ."|";
            }
            $y++;
        }
    
        unset($value);
    
        $pattern = "/((((http|ftp|https)+(:)+(\/{2})))?+(www\.)?(((". $DomNames .")+\.)+(". $DomExt .")+(\.uk)?(:[0-9]+)?((\/([~0-9a-zA-Z\#\+\%@\.\/_-]+))?(\?[0-9a-zA-Z\+\%@\/&\[\];=_-]+)?)?))\b/imu";
        if(preg_match($pattern, $domain)){
            echo "Domain match!<br />";
        } else {
            echo "Domain not match!<br />";
        }
    }
    
    testDomain("www.test1.com"); //match
    testDomain("test1.com"); //match
    testDomain("http://www.test1.co.uk"); //match
    testDomain("test1.com/info.php?who=you"); //match
    testDomain("www.google.com"); //no match
    
    ?>
    

    【讨论】:

      【解决方案4】:

      如何让它工作

      $arrDomainNames = array("test1.com","test2.org","test3.co.uk","test3.net");
      

      没有

      $arrDomainExt = array("com","co");
      

      【讨论】:

        猜你喜欢
        • 2022-01-28
        • 1970-01-01
        • 2015-03-28
        • 2014-03-21
        • 2017-02-27
        • 1970-01-01
        • 1970-01-01
        • 2014-01-08
        • 2021-05-06
        相关资源
        最近更新 更多