If your programs require that the end users enter a url,
you have to make sure it is being input in the correct
format. Especially if this URL is going to be displayed
on a webpage as a link. If the URL is not entered with
the "http://" at the beginning, the link won't work. Try it
for yourself and you will see that hypertext links don't work
if there is no "http://" at the beginning of the URL.
This utility takes away the need for instructions to the user
or for you to try and figure out the RegExp to check this. I've
done it for you.
Here is how you use this utility:
Either cut and paste this subroutine into your own script or
"require" this file by placing it in the same directory as your
script and placing the following line anywhere before the
subroutine is first called:
require 'format_url.pl';
Now you are ready to use it. The subroutine needs one parameter
passed to it. This is the URL you want the format checked for.
The subroutine will take any URL that does not have or is
incomplete (ever typed "htp://www.foo.com" or "http:/www.foo.com"
by mistake?) and return a properly formatted URL.
$good_url = format_url($check_url);
$good_url will be properly formatted: http://www.foo.com/whatever
What errors will it pick up? Here is list of what the subroutine
will trap and correct:
- www.foo.com
- /www.foo.com
- //www.foo.com
- ://www.foo.com
- p://www.foo.com
- tp://www.foo.com
- ttp://www.foo.com
- http://www.foo.com
....or an combination of errors prior to the last forward slash.
Examples of valid calling formats:
$good_url = format_url($check_url);
$good_url = format_url("$check_url/$variable1/$variable2");
NOTE: This subroutine does not check to see if the referenced file
actually exists, it only checks to see if the URL has the "http://"
at the front of it.
|
S T O P ! ! !
This sub-routine does not support secure URL's. That is, URL's that have "https://"
instead of "http://". Therefore, use your skills to create an alternate sub-routine
to handle just secure URL's or if you are REALLY ambitious, use this sub-routine
to create a new sub-routine that handles both secure and non-secure URL's. It's only
mildly tricky!!
|
|