Solved

regular expression help, validate url and extension

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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

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…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

728 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