views:

63

answers:

5

In PHP I'm receiving a string from the user for a local file directory $path

I want to know if they've included the trailing slash (/) or not. I would like this to be cross platform so I'd like to use the PHP constant DIRECTORY_SEPARATOR

My failed attempts include trying to do a preg_match like

preg_match("/" . DIRECTORY_SEPARATOR . "$/", $path);

I basically just want an elegant way to test if the string ends with the DIRECTORY_SEPARATOR.

+4  A: 

To fix your regexp:

preg_match("/" . preg_quote(DIRECTORY_SEPARATOR) . "$/", $path);

But there may be simpler ways to achieve your goal:

rtrim($path,DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
mvds
+1 Nice neat rtrim solution. (Like mine it will fail if DIRECTORY_SEPARATOR isn't a single character, but I've yet to see an OS that would cause problems.)
middaparka
I was thinking about not using `/` for regexp delimiter, but indeed, who knows what DIRECTORY_SEPARATOR may be invented in years to come...
mvds
btw this will not fail on multi-char DIRECTORY_SEPARATOR, for it will chop of the entire separator. (well, ok, if the last char of the filename would be a char contained in the multi-char DIRECTORY_SEPARATOR... but this would be very obscure)
mvds
True, perhaps "fail" is a bit excessive. How about "Its behaviour would be undefined". :-)
middaparka
This is perfect! Short, sweet and accomplishes the task.
nvoyageur
@mvds. Given it's PHP, they'd use `\\`, just like in namespaces, and no one's used it before... oh... wait a minute...
Marc B
A: 

A trival way of working this out would be to use...

$sepPresent = $path{strlen($path) - 1} == DIRECTORY_SEPARATOR ? true : false;

...which would be a lot more efficient than a regular expression. Then again, this will fail if DIRECTORY_SEPARATOR isn't a single character.

middaparka
`substr($path,-1)` would in turn be more efficient than `$path{strlen($path) - 1}`
mvds
A: 

I think depending on platform you can end up with an invalid regular expression on the first argument.

Your solution is elegant per design. Nice coding. But remember that directory separators usually are special characters. It may needs some escaping.

EDIT #1 I liked the proposed solution

$sepPresent = $path{strlen($path) - 1} == DIRECTORY_SEPARATOR ? true : false;

And to work it out I suggest:

$sepPresent = $path{strlen($path) - strlen(DIRECTORY_SEPARATOR)} === DIRECTORY_SEPARATOR ? true : false;
Dave
This will not work for multi-char, since `$path{..}` gets 1 char only. better use `substr($path,-strlen(DIRECTORY_SEPARATOR))`.
mvds
yeap, my bad...
Dave
A: 

Could also use array access notation

$endsInDS = $str[strlen($str)-1] === DIRECTORY_SEPARATOR;
Gordon
A: 

what's wrong with a simple substr($path,-1)==DIRECTORY_SEPARATOR ?

stillstanding
it would do the trick but once again, i think the asker fears directory separator might change in the future. It might not be more than one character of length, but more, ie, ::
fabjoa
unless the world is moving backward, i see no reason for a world asking for more characters to type on the command line
stillstanding