Last active
October 30, 2016 13:28
-
-
Save onigetoc/c9ff6732aafff829cd241d2adb841189 to your computer and use it in GitHub Desktop.
This file contains 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 | |
//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