Search code examples
phppreg-match

php set path using preg_match?


I'm creating a very simple file browser, and am trying to limit the path to be /local/ANYTHING, but NOT /local it's self. (where ANYTHING can be any subfolder).

Currently I'm using:

$path = (strpos($path, '/local/') !== false) ? $path : "/local/ud";

But this accepts /local/ as the path. The aim is if its not /local/ANYTHING then set it to /local/ud.

I've tried to use preg_match() and set it as /local/* but again this allow access to /local.

Is there any way to say the path can only be /local/ANYTHING while excluding /local itself?


Solution

  • This forces at least one letter after /

    preg_match("/\/local\/\w.*/", $input, $output);
    

    http://www.phpliveregex.com/p/fB2

    Edited to add \w to make sure you cant be at /local/ <- there is supposed to be a space after /

    Maybe this is even better? "/\/local\/[\w|\d].*/" One letter or digit then anything