?
Solved

regular expression help, validate url and extension

Posted on 2004-04-04
3
Medium Priority
?
1,569 Views
Last Modified: 2012-05-04
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
0
Comment
Question by:HeadAcheMike
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 5

Expert Comment

by:crazycomputers
ID: 10752472
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);
0
 
LVL 25

Accepted Solution

by:
Marcus Bointon earned 600 total points
ID: 10752596
Rather than going too far into the whole regex thing (which can get very, very complex with a true URL spec), you can use the built-in parse_url and pathinfo functions to extract data from the URL and it will work with URLs that use any scheme (FTP, HTTP, HTTPS etc), user IDs and passwords, odd port numbers etc, and will also let you ignore # or query parts. the pathinfo function saves you from having to dismantle the path by hand too:

$urlparts = parse_url($url);
$pathinfo = pathinfo($urlparts['path']); //Should probably check that the URL actually has a path part
if (preg_match('/^(jpe?g|gif|png)$/',$pathinfo['extension']) {
  ...
}

This is probably much easier than trying to do this yourself, and will almost certainly be faster than a big regex.
0
 
LVL 2

Author Comment

by:HeadAcheMike
ID: 10782065
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
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question