[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

regular expression help, validate url and extension

Posted on 2004-04-04
3
Medium Priority
?
1,571 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 i…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

649 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