1

如何将其更改为允许对 Vine URL 使用 HTTP 或 HTTPS?

$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";
}
4

3 回答 3

3

您可以使用该parse_url功能,它将 URL 分解为其组件,从而更容易单独匹配每个组件:

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

scheme然后,您可以检查 ifhostpath匹配:

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
于 2014-12-11T17:58:29.303 回答
1

Just take out the "https://" and change your if statement a bit... like this:

$vineURL = 'vine.co/v/';
if(stripos($user_input_value, $vineURL) !== false) {
    echo "This is a vine URL";
} else {
    echo "This is not a vine URL";
}
于 2014-12-11T17:47:41.453 回答
0

像这样的用户正则表达式

if (preg_match("/^http(s)?:\/\/(www\.)?vine\.co\/v\//", $url)) {
    echo "This is a vine URL";
} else {
    echo "This is not a vine URL";
}
于 2014-12-11T17:58:00.673 回答