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
.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With