parse_url
Posted: Sat Aug 11, 2007 12:27 pm
I have a form where a user is suppose to enter his site URL. Just out of habit I always type the http:// in front of all URL's. However it has come to my attention that not all users are as habitual as I am.
So I created a little code that checks to see if the http is on the URL, if it is'nt, it will concatenate the http:// with the URL the user entered. I figured parse_url() would be a function to use and this is the code I came up with:
Now if the http:// is present on the $site string, http://www.google.com is $site_array['host'] - if the http:// is not present [ie. the code above] http://www.google.com is defined as $site_array['path']. Why?
So I created a little code that checks to see if the http is on the URL, if it is'nt, it will concatenate the http:// with the URL the user entered. I figured parse_url() would be a function to use and this is the code I came up with:
Code: Select all
<?php
$site = "www.google.com";
$site_array = parse_url($site);
if($site_array['scheme'] == "") {
$site = "http://".$site_array['path'];
}
echo $site;
?>