douhu3424 2014-12-11 17:43
浏览 31
已采纳

如何使用PHP验证Vine URL并允许HTTP或HTTPS

How can this be changed to allow either HTTP or HTTPS for a Vine URL?

$vineURL = 'https://vine.co/v/';
$pos = stripos($url_input_value, $vineURL);

if ($pos === 0) {
    echo "The url '$url' is a vine URL";
}
else {
    echo "The url '$url' is not a vine URL";
}
  • 写回答

3条回答 默认 最新

  • douzhan2027 2014-12-11 17:58
    关注

    You can use the parse_url function, it breaks the URL into its components making it easier to match each component individually:

    var_dump(parse_url("https://vine.co/v/"));
    // array(3) {
    //   ["scheme"]=>
    //   string(4) "http"
    //   ["host"]=>
    //   string(7) "vine.co"
    //   ["path"]=>
    //   string(3) "/v/"
    // }
    

    You can then just check if scheme, host and path match:

    function checkVineURL($url) {
        $urlpart = parse_url($url);
        if($urlpart["scheme"] === "http" || $urlpart["scheme"] === "https") {
            if($urlpart["host"] === "vine.co" || $urlpart["host"] === "www.vine.co") {
                if(strpos($urlpart["path"], "/v/") === 0) {
                    return true;
                }
            }
        }
        return false;
    }
    checkVineURL("https://vine.co/v/");     // true
    checkVineURL("http://vine.co/v/");      // true
    checkVineURL("https://www.vine.co/v/"); // true
    checkVineURL("http://www.vine.co/v/");  // true
    checkVineURL("ftp://vine.co/v/");       // false
    checkVineURL("http://vine1.co/v/");     // false
    checkVineURL("http://vine.co/v1/");     // false
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)