![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
King of Portal
|
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
__________________
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 |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#3 |
|
King of Portal
|
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 |
|
|
|
|
|
#4 |
|
Expert Programmer
|
I believe this is what you are looking for:
ereg('^([[:alnum:]_/\-])*/?([[:alnum:]_\-]+\.php)?$', $str);Last edited by titaniumdecoy; Aug 5th, 2007 at 6:23 PM. |
|
|
|
|
|
#5 |
|
King of Portal
|
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
__________________
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 |
|
|
|
|
|
#6 |
|
Expert Programmer
|
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. |
|
|
|
|
|
#7 |
|
King of Portal
|
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 |
|
|
|
|
|
#8 |
|
King of Portal
|
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 |
|
|
|
|
|
#9 |
|
King of Portal
|
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 |
|
|
|
|
|
#10 |
|
King of Portal
|
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|