我输入了访问者必须输入网址的字段,有些可能只输入www.some_site.com
或http://www.some_site.com
或https://www.some_site.com
甚至是some_site.com
。
我希望无论他们输入的是输出http://www.some_site.com
我会使用str_replace
但是如何使用。
〜任何帮助或想法。
答案 0 :(得分:2)
查看PHP的parse_url功能。
if ($result['scheme'] == '' || $result['scheme'] == 'https') {
$result['scheme'] = 'http';
}
然后使用http_build_url
将其重新组合在一起答案 1 :(得分:1)
您可以使用以下代码
//$url = "https://www.google.com";
$url = "http://www.google.com";
//$url = "google.com";
echo convertUrl( $url);
function convertUrl ( $url ) {
$parts = parse_url($url);
//print_r( $parts);
$returl = "";
if ( empty($parts['scheme']) ) {
$returl = "http://" . $parts['path'];
} else if ( $parts['scheme'] == 'https') {
$returl = "http://" . $parts['host'];
} else {
$returl = $url;
}
return $returl;
}
答案 2 :(得分:0)
public function unifyUrl($sUrl){
$exp = explode('://', $sUrl);
if(count($exp) > 1)//means we have http or https
$url = $exp[1];
else
$url = $exp[0];
if(!preg_match('/^www\./', $url))//we are lack of www. in the beginning
$url = 'www.'.$url;
return 'http://'.$url;
}
答案 3 :(得分:0)
$myURL = 'www.some_site.com/something_else/else/else.php'; //the normal URL
$myURL = explode('.some_site.com', $myURL); //break down the url by '.some_site.com'
$myURL = 'http://www.some_site.com/' . $myURL[1]; //add 'http://www.some_site.com/' and then add 'something_else/else/else.php'
echo $myURL;// OUTPUT: http://www.some_site.com//something_else/else/else.php