Skip to content

Instantly share code, notes, and snippets.

@wisecrab
Created December 21, 2015 18:50
Show Gist options
  • Save wisecrab/8503d01d917af32985f7 to your computer and use it in GitHub Desktop.
Save wisecrab/8503d01d917af32985f7 to your computer and use it in GitHub Desktop.
Simple function to prepare URL's with either http or https
<?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