Created
December 21, 2015 18:50
-
-
Save wisecrab/8503d01d917af32985f7 to your computer and use it in GitHub Desktop.
Simple function to prepare URL's with either http or https
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function prepare_url($string) { | |
//Check if the string starts with http or https | |
if (substr($string, 0, 7) == "http://" OR substr($string, 0, 8) == "https://") { | |
$url = $string; | |
} else { | |
//Remove any presence of http:// or https:// from the link | |
$string = str_replace("://", "", $string); | |
$string = str_replace("https", "", $string); | |
$string = str_replace("http", "", $string); | |
//Parse the url and see if t returns with a secured url | |
$url_check = parse_url($string); | |
if($url_check['scheme'] == 'https'){ | |
$url = 'https://'.$string; | |
} else { | |
$url = 'http://'.$string; | |
} | |
} | |
return $url; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment