Solved

regular expression help, validate url and extension

Posted on 2004-04-04
3
1,562 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:
Squinky 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now