Zim*_*m3r 3 php parsing parse-url
根据以下代码,如果$host_name类似于example.comPHP返回通知:Message: Undefined index: host但在http://example.comPHP返回的完整URL上example.com.我尝试使用FALSE和NULL语句但是没有用.
$host_name = $this->input->post('host_name');
$parse = parse_url($host_name);
$parse_url = $parse['host'];
Run Code Online (Sandbox Code Playgroud)
如何修改脚本以接受example.com并将其返回?
升级你的PHP.
5.4.7 Fixed host recognition when scheme is ommitted and a leading component separator is present.
手动添加方案: if(mb_substr($host_name, 0, 4) !== 'http') $host_name = 'http://' . $host_name;
您可以检查该方案是否存在使用filter_var,如果不存在,则前置一个
$host_name = 'example.com';
if (!filter_var($host_name, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED)) {
$host_name = 'http://' . $host_name;
}
$parse = parse_url($host_name);
var_dump($parse);
array(2) {
["scheme"]=>
string(4) "http"
["host"]=>
string(11) "example.com"
}
Run Code Online (Sandbox Code Playgroud)