如何将其更改为允许对 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";
}
如何将其更改为允许对 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";
}
您可以使用该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
然后,您可以检查 ifhost
并path
匹配:
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
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";
}
像这样的用户正则表达式
if (preg_match("/^http(s)?:\/\/(www\.)?vine\.co\/v\//", $url)) {
echo "This is a vine URL";
} else {
echo "This is not a vine URL";
}