![]() |
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
|
Have you looked at the pathinfo function?
|
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.
|
I believe this is what you are looking for:
:
ereg('^([[:alnum:]_/\-])*/?([[:alnum:]_\-]+\.php)?$', $str); |
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 |
I believe the following regular expression matches your description:
:
^([[:alnum:]_][[:alnum:]_\-]*/?)*?([[:alnum:]_]+[[:alnum:]_\-]*\.php)?$ |
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'; |
What exactly is it the '?' character does? The PHP documentation's description of it doesn't make sense to me.
|
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)?$/'; |
oh I see you're using the POSIX expressions rather than the PERL expressions, yeah that's just totally lost on me *_*
|
| All times are GMT -5. The time now is 9:35 PM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC