Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 5th, 2007, 2:43 PM   #1
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 403
Rep Power: 3 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
POSIX compliant portable pathname

I haven't had much experience with regular expressions because I tend to avoid them like the plague. However, for this particular application it appeared to fit my needs well. In any case I wanted to create a function that would verify a pathname with the following structure:
whatever/whatever/whatever/........../whatever.php
It should follow the guidelines specified here, and my own stipulations:
  1. The pathname need not end in a filename
  2. If a filename is present it must end in '.php'
This was my solution:[PHP]preg_match('/^([a-z0-9_][a-z0-9\-_]*\/{1,1})*([a-z0-9_][a-z0-9\-_]*)*(\.php){0,1}$/iD', $text);[/PHP]Due to my inexperience I'd appreciate any regular expression gurus here on the forum highlighting where I might have missed something in my regular expression that would cause it to behave beyond the scope of the specified parameters. Thanks in advance for the help.
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis
grimpirate is offline   Reply With Quote
Old Aug 5th, 2007, 3:09 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Have you looked at the pathinfo function?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Aug 5th, 2007, 5:35 PM   #3
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 403
Rep Power: 3 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
I did indeed DaWei, but there are certain things about its behavior which didn't seem correct. Take for instance the basename function, if I use that but the file doesn't have a '.***' extension it returns what I would interpret as a directory name. If the pathname starts with a '/' it also considers it ok. For my purposes those things don't work. I tried using pathinfo at first, but with certain pathnames the behavior is kinda screwy and I have to worry about these exceptions which kind of extends my code needlessly I think. I may not be using it properly I have no clue, but thus far the preg_match is working exactly as I want. Also, pathinfo doesn't check for the non-compliant characters and their positions. I figured with preg_match I could do all of these things in one shot rather than having to code for all the possibilties that transpire using pathinfo. I'm not one for abusing regular expressions especially since I don't really feel comfortable using them, but I do recognize their versatility in certain applications.
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis
grimpirate is offline   Reply With Quote
Old Aug 5th, 2007, 6:04 PM   #4
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 837
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
I believe this is what you are looking for:

ereg('^([[:alnum:]_/\-])*/?([[:alnum:]_\-]+\.php)?$', $str);
Both this regular expression and the one you provided will only allow a period before the filename. Your regular expression will match a path with a filename but no extension, such as "whatever/whatever". It will also reject paths that begin with a /. Other than that, it appears okay, although verbose. Also, why not allow file/directory names to begin with a -, as this can occur in URIs?

Last edited by titaniumdecoy; Aug 5th, 2007 at 6:23 PM.
titaniumdecoy is offline   Reply With Quote
Old Aug 6th, 2007, 1:09 AM   #5
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 403
Rep Power: 3 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
I don't want path names to begin with a '/' that's why I provided the example; I should've stated that explicitly. The POSIX standard doesn't allow file/directories to begin with a hyphen. I think the portion of your regular expression '([[:alnum:]_/\-])' allows for things pathnames which could have something like '//////' which is something I'm also not allowing (assuming I'm reading it right, again I'm no regex genius). Let me give some explicit examples of the types of pathnames I'm interested in validating:
VALID EXAMPLES
whatever/*****/whatever
whatever/*****/whatever/
whatever
whatever/
whatever/whatever.php
whatever.php
_whatever
_whatever/
INVALID EXAMPLES
////////whatever////////
./
../
.
..
-whatever
-whatever/
whatever.txt
/whatever
My regular expression actually doesn't fit those needs so I do need to modify it. The 'whatever' portions don't actually mean whatever, they should be comprised only of alphanumerics, hyphens, and/or underscores exclusively and can only be broken up by a single '/' character. The whatever segments must have a length of at least one character. If no filename is present then the pathname can terminate with a '/' or not. If a filename is present it must end in '.php'. I think I've described all the rules here.
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis
grimpirate is offline   Reply With Quote
Old Aug 6th, 2007, 1:26 AM   #6
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 837
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
I believe the following regular expression matches your description:

^([[:alnum:]_][[:alnum:]_\-]*/?)*?([[:alnum:]_]+[[:alnum:]_\-]*\.php)?$

Last edited by titaniumdecoy; Aug 6th, 2007 at 1:50 AM.
titaniumdecoy is offline   Reply With Quote
Old Aug 6th, 2007, 1:33 AM   #7
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 403
Rep Power: 3 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
I think I've got it now by splitting it into two cases and then ORing the result
$regExpDir = '/^([a-z0-9_][a-z0-9\-_]*\/)*([a-z0-9_][a-z0-9\-_]*\/{0,1})+$/iD';
$regExpFil = '/^([a-z0-9_][a-z0-9\-_]*\/)*([a-z0-9_][a-z0-9\-_]*\.php)+$/iD';
return preg_match($regExpDir, $fileName) || preg_match($regExpFil, $fileName);
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis
grimpirate is offline   Reply With Quote
Old Aug 6th, 2007, 1:36 AM   #8
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 403
Rep Power: 3 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
What exactly is it the '?' character does? The PHP documentation's description of it doesn't make sense to me.
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis
grimpirate is offline   Reply With Quote
Old Aug 6th, 2007, 1:46 AM   #9
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 403
Rep Power: 3 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
Actually I tried your expression out and received the following error message:
Warning: preg_match() [function.preg-match]: Unknown modifier '?' in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\test.php on line 9
Did I implement it incorrectly?
$regExp = '/^([[:alnum:]_][[:alnum:]_\-]*/?)*(\.php)?$/';
echo preg_match($regExp, $fileName);
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis
grimpirate is offline   Reply With Quote
Old Aug 6th, 2007, 1:56 AM   #10
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 403
Rep Power: 3 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
oh I see you're using the POSIX expressions rather than the PERL expressions, yeah that's just totally lost on me *_*
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis
grimpirate is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:41 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC