PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
seodevhead
Forum Regular
Posts: 705 Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL
Post
by seodevhead » Mon Oct 31, 2005 8:44 pm
Hello... I found this
regular expression for validating
URL's which I want to use in my php script:
Code: Select all
/^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\//i
I am not too good with reg ex's, but my current script in which I validate the URL has this as the validator conditional....
Code: Select all
if(eregi('^([[:alnum:]\-\.])+(\.)([[:alnum:]]){2,4}([[:alnum:]/+=%&_\.~?\-]*)$', $_POST['homepage']))
How do I go about getting the
first regular expression above inside the
if(eregi(...... , $_POST['homepage']))
Thanks for all your help... it's truly appreciated!!![/b]
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Mon Oct 31, 2005 8:47 pm
use
preg_match() instead, it's faster, and your found pattern was intended for it.
seodevhead
Forum Regular
Posts: 705 Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL
Post
by seodevhead » Mon Oct 31, 2005 8:54 pm
May I ask how I go about making the if statement using this regular expression? That is what I am having difficulty with. Thanks for your help!
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Mon Oct 31, 2005 8:57 pm
place the found pattern into a string, follow the example(s) from the
preg_match() page..
seodevhead
Forum Regular
Posts: 705 Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL
Post
by seodevhead » Mon Oct 31, 2005 9:00 pm
Hey that's a cool function... but unfortunately I have to use eregi .
Do you know how to write the if statement I need using eregi and the first regular expression? THanks!
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Mon Oct 31, 2005 9:06 pm
why do you have to use eregi? preg_match performs the same functionality, but twice as fast and doesn't require modifying your pattern to suit it..
seodevhead
Forum Regular
Posts: 705 Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL
Post
by seodevhead » Mon Oct 31, 2005 9:12 pm
It's for a school assignment, and eventhough it would work fine for the program, my prof is super picky when it comes to adding code into our projects that he hasn't taught....
ed209
Forum Contributor
Posts: 153 Joined: Thu May 12, 2005 5:06 am
Location: UK
Post
by ed209 » Fri Feb 17, 2006 4:32 pm
did you get this to actually work? I can't seem to get any URLs to validate...
Code: Select all
if(preg_match("/^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\//i", $url)){
return true;
}else{
return false;
}
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Fri Feb 17, 2006 4:40 pm
What are you trying to match against ed209?