Link to home
Start Free TrialLog in
Avatar of HeadAcheMike
HeadAcheMike

asked on

regular expression help, validate url and extension

hi,

i have an expression that validates URLs but i need it modified so that it only allows certain file extenstions, or if you have another fresh example that will do fine

i need an expression that will only allow a url with a file name with one of the following extensions, jpg, jpeg, gif, png

for example

http://domain.com/picture.jpg - valid
http://domain.com/picture.php - invalid

http://domain.co.uk/folder/picture.jpg - valid
http://domain.co.uk/folder/ - invalid

etc etc

here is the function i currently have:

function valiurl($url) {
 if (eregi("^((ht|f)tp://)((([a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3}))|(([0-9]{1,3}\.){3}([0-9]{1,3})))((/|\?)[a-z0-9~#%&'_\+=:\?\.-]*)*)$", $url)) {
  return true;
 }
 else {
  return false;
 }
}

any help will be much appreciated, thank you
Avatar of crazycomputers
crazycomputers

You generally don't need something that complex unless you need PERFECT detection.  Try something like this:

eregi("^(ht|f)tp://.+?\..+?/.+?\.(jpe?g|gif|png)$", $url);
ASKER CERTIFIED SOLUTION
Avatar of Marcus Bointon
Marcus Bointon
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of HeadAcheMike

ASKER

Thanks to both and sorry for the delay responding

i will look at both suggested solutions tonight and award the points to the one that best suits my system

Thanks again, Mike