URL Validation => eregi

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

Post Reply
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

URL Validation => eregi

Post by seodevhead »

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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

use preg_match() instead, it's faster, and your found pattern was intended for it. :)
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

Post by seodevhead »

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!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

place the found pattern into a string, follow the example(s) from the preg_match() page..
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

Post by seodevhead »

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!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

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.. :?
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

Post by seodevhead »

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.... :(
User avatar
ed209
Forum Contributor
Posts: 153
Joined: Thu May 12, 2005 5:06 am
Location: UK

Post by ed209 »

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;
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

What are you trying to match against ed209?
Post Reply