我想提出一个JSON验证,需要验证URL以http://或https://开头PHP验证(两个条件)
if(preg_match("/^[http://][a-zA-Z -]+$/", $_POST["url"]) === 0)
if(preg_match("/^[https://][a-zA-Z -]+$/", $_POST["url"]) === 0)
我错在synatx,还我应该如何在同一个语句中同时包含(http和https)?
谢谢!
我想提出一个JSON验证,需要验证URL以http://或https://开头PHP验证(两个条件)
if(preg_match("/^[http://][a-zA-Z -]+$/", $_POST["url"]) === 0)
if(preg_match("/^[https://][a-zA-Z -]+$/", $_POST["url"]) === 0)
我错在synatx,还我应该如何在同一个语句中同时包含(http和https)?
谢谢!
$isHttps = (isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) ? true : false;
可以使用parse_url
<?php
$url = parse_url($_POST["url"]);
if($url['scheme'] == 'https'){
// is https;
}else if($url['scheme'] == 'http'){
// is http;
}
// try to do this, so you can know what is the $url contains
echo '<pre>';print_r($url);echo '</pre>';
?>
OR
<?php
if (substr($_POST["url"], 0, 7) == "http://")
$res = "http";
if (substr($_POST["url"], 0, 8) == "https://")
$res = "https";
?>
如果要检查你的字符串的http://开头或HTTPS ://而不必担心整个URL的有效性,只要做到这一点:
<?php
if (preg_match('`^https?://.+`i', $_POST['url'])) {
// $_POST['url'] starts with http:// or https://
}
希望此链接将帮助您: https://stackoverflow.com/questions/6427530/regular-expression-pattern-to-match- url-with-or-without-http-www –
你可以使用这个:https://regex101.com/r/Ciafhq/1 – C2486