Skip to content

Instantly share code, notes, and snippets.

@onigetoc
Last active October 30, 2016 13:28
Show Gist options
  • Save onigetoc/c9ff6732aafff829cd241d2adb841189 to your computer and use it in GitHub Desktop.
Save onigetoc/c9ff6732aafff829cd241d2adb841189 to your computer and use it in GitHub Desktop.
<?php
//Write your PHP code here
$url = "http://stackoverflow.com/questions/6240414/add-http-prefix-to-url-when-missing";
$scheme = parse_url($url, PHP_URL_SCHEME);
if (empty($scheme)) {
$url = 'http://' . ltrim($url, '/');
}
$urlnohttp = preg_replace("(^https?://)", "", $url );
echo "URL: ". $url; // URL: http://stackoverflow.com/questions/6240414/add-http-prefix-to-url-when-missing
echo "<br>";
echo "scheme: ". $scheme; // scheme: http
echo "<br>";
echo "urlnohttp: ". $urlnohttp; // urlnohttp: stackoverflow.com/questions/6240414/add-http-prefix-to-url-when-missing
echo "<br>";
// FOR MY NEED
if ($scheme) {
echo "Get http or https<br>"; // Get http or https
}
//$parse = parse_url($url);
//print "parse: ". $parse['host']; // prints 'stackoverflow.com'
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment