稍后继续优化
<?php
function addWww($url){
$parse = parse_url($url);
$scheme = isset($parse['scheme'])?$parse['scheme']:'';
$host = isset($parse['host'])?$parse['host']:'';
$path = isset($parse['path'])?$parse['path']:'';
$query = isset($parse['query'])?$parse['query']:'';
$fragment = isset($parse['fragment'])?$parse['fragment']:'';
preg_match('/^(([www]{3})|([\w]{1,}))[\.][\w]*?[\.][com|cn|com.cn|cc|net]{2,}/',$host,$match);
if($match){
$resUrl = $url;
}else{
$resUrl = $scheme.'://'.'www.'.$host.$path.'?'.$query.'#'.$fragment;
}
return $resUrl;
}