为什么我的代码出现"意外的正则表达式"错误?

0 php regex

目标是将当前工作目录拆分为,/clients/并查看用户是否在

[something]/clients/[their username]/[something]

例如,目标是输入:

cwd = "/volumes/raid0/www/clients/mikey/test_folder/"
$session->username = "mikey"
Run Code Online (Sandbox Code Playgroud)

回来

$authorized = true
Run Code Online (Sandbox Code Playgroud)

我希望这能识别UNIX和Windows路径,因此它应该寻找"/"或"\".假设文件名不包含这些字符.

此外,该isAdmin()位应该授予管理员访问所有目录的权限.

现在,PHP说:

警告:第69行的c:\ apache\htdocs\clients\mikey\index.php中出现意外的正则表达式错误(8)

这是现在的代码.(第69行在评论中注明.)

if($session->isAdmin())
{
    $authorized = true;
} 
else 
{
  // split cwd at the first instance of /clients/
  $dir = spliti('%(\/|\\)clients(\/|\\)%',getcwd(),2); //this is line 69
  if(count($dir) == 2) // if /clients/ was in cwd
  {
    // check if the second piece of cwd starts with the username.
    $authorized = (preg_match('/^'.$session->username.'//*.$/', $dir[1]));
  } 
  else 
    $authorized = false;
}
Run Code Online (Sandbox Code Playgroud)

sou*_*rge 6

这里不需要使用正则表达式,而是在另一个字符串中查找字符串.所以这将是这样的:

$isAuthorized = strpos(str_replace('\\', '/', getcwd()), "/clients/$username/") !== FALSE;
Run Code Online (Sandbox Code Playgroud)