parse_url为url返回空字符串:'www.example.com'

Why is parse_url returning empty string in this case?

<?php
$url='www.example.com';
$var= parse_url($url,PHP_URL_HOST);
print_r($var);

The string is interpreted as relative URL:

// print_r(parse_url('www.vtechpcsupport.com'))
Array
(
    [path] => www.vtechpcsupport.com
)  

This is due to fact that www.vtechpcsupport.com isn't really a URL since it missing the scheme part (HTTP or so), try it like this:

$url = 'http://www.vtechpcsupport.com';
$var = parse_url($url,PHP_URL_HOST);
print($var);

This is because www.vtechpcsupport.com is not a complete URL.

You are specifying PHP_URL_HOST so the function tries to extract just the host part of the URL, which doesn't exist as without a protocol being given the URL is treated as being relative -- so what you want to be the host name is interpreted as a (relative) path.

Try using:

$url='http://www.vtechpcsupport.com';

and you should get the behaviour you expect.