Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   PHP (http://www.programmingforums.org/forum29.html)
-   -   POSIX compliant portable pathname (http://www.programmingforums.org/showthread.php?t=13705)

grimpirate Aug 5th, 2007 2:43 PM

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.

DaWei Aug 5th, 2007 3:09 PM

Have you looked at the pathinfo function?

grimpirate Aug 5th, 2007 5:35 PM

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.

titaniumdecoy Aug 5th, 2007 6:04 PM

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?

grimpirate Aug 6th, 2007 1:09 AM

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.

titaniumdecoy Aug 6th, 2007 1:26 AM

I believe the following regular expression matches your description:

:

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

grimpirate Aug 6th, 2007 1:33 AM

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);


grimpirate Aug 6th, 2007 1:36 AM

What exactly is it the '?' character does? The PHP documentation's description of it doesn't make sense to me.

grimpirate Aug 6th, 2007 1:46 AM

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);


grimpirate Aug 6th, 2007 1:56 AM

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