Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if directory path ends with DIRECTORY_SEPARATOR

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.

like image 893
Shane Stillwell Avatar asked Dec 04 '22 11:12

Shane Stillwell


1 Answers

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;
like image 133
mvds Avatar answered Dec 27 '22 23:12

mvds