Solved

regular expression help, validate url and extension

Posted on 2004-04-04
3
1,565 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
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 150 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

773 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