Skip to content

Instantly share code, notes, and snippets.

@Kaiyuan
Created January 8, 2014 11:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kaiyuan/8315085 to your computer and use it in GitHub Desktop.
Save Kaiyuan/8315085 to your computer and use it in GitHub Desktop.
格式化 URL,自动判断添加 http 或 https。
<?php
function format_url($url,$slash)
{
/*
* 规范化 URL
* 判断是否使用 HTTPS 链接,当是 HTTPS 访问时候自动添加
* 自动添加链接前面的 http://
* $slash 是判断是否要后面添加斜杠
*/
if (substr($url,0,4)!='http') {
@$if_https = $_SERVER['HTTPS']; //这样就不会有错误提示
if ($if_https) { //如果是使用 https 访问的话就添加 https
$url='https://'.$url;
} else {
$url='http://'.$url;
}
}
if ($slash) {
$url = rtrim($url,'/').'/';
}
return $url;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment