Checking for correctly formatted URLs in a form
I'm having no joy with this formhandler with the following Pattern:
Error: Totally Ignores Pattern and writes clickable URL link
"(http://)?([[:space:]]+)([[:alnum:].,-_?/&=])"
As published in Visual Quickstart guide PHP BY LARRY ULLMAN
peachpit.com
<?php
/ This page receives and handles the data generated by "form.html". /
if (($Array["FirstName"]) AND ($Array["LastName"])) {
$Array["Name"] = $Array["FirstName"] . " " . $Array["LastName"];
} else {
print ("Please enter your first and last names.<BR>\n");
}
$Pattern = "(http://)?([[:space:]]+)([[:alnum:].,-_?/&=])";
$Replace = "<a href=\"http://\2\3\" target=\"_new\">\2\3</a>";
$Array["URL"] = eregi_replace($Pattern, $Replace, $Array["URL"]);
print ("Your submission--$Array[URL]--has been received!<BR>\n");
?>
This one published by Wrox.com
This pattern clearly fails in a form too:
("[a-zA-Z0-9]+://[ ]+$")
However works here in this sample using 'Regexp'
<?php
//urlcheck.php
function urlcheck($intext) {
$theresults = ereg("[a-zA-Z0-9]+://[ ]+$", $intext, $trashed);
if ($theresults) { $isamatch = "Yes"; } else { $isamatch = "No"; }
print ("URL check for \"$intext\": $isamatch<br>\n");
}
Main
urlcheck("www.whateverhost.com");
urlcheck("http://www.whateverhost.com/");
urlcheck("http://www.bogus host with spaces.com/");
urlcheck("http://www.abcd.cc/filenames should lack spaces.html");
urlcheck("http://www.abcd.cc/filenames%20should%20lack%20spaces.html");
urlcheck("ftp://ftp2.somegiantcompany.com/pub/largefiles/");
urlcheck("telnet://mytelnethost.org:8888");
?>
If you come up with a solution just post here.
Once I have found a solution I'll post back