【问题标题】:Why do I get an "unexpected regex" error with my code?为什么我的代码会出现“意外的正则表达式”错误?
【发布时间】:2008-12-17 16:05:40
【问题描述】:

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

[某事]/clients/[他们的用户名]/[某事]

例如,目标是输入:

cwd = "/volumes/raid0/www/clients/mikey/test_folder/"
$session->username = "mikey"

返回

$authorized = true

我希望它能够识别 UNIX 和 Windows 路径,因此它应该查找“/”或“\”。假定文件名不包含这些字符。

另外,isAdmin() 位应该允许管理员访问所有目录。

现在,PHP 说:

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

这里是代码。 (第 69 行在 cmets 中注明。)

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;
}

【问题讨论】:

    标签: php regex


    【解决方案1】:

    这里不需要使用正则表达式,你是在另一个字符串中寻找一个字符串。所以会是这样的:

    $isAuthorized = strpos(str_replace('\\', '/', getcwd()), "/clients/$username/") !== FALSE;
    

    【讨论】:

      【解决方案2】:

      这个简单的正则表达式变体是:

      • 在"/" 或"\" 处拆分字符串
      • 在结果数组中搜索"clients"
      • 如果找到,检查下一个位置的元素是否等于用户名

      在 PHP 中(尽管未经测试):

      function isAuthorized($session)
      {
        $authorized = $session->isAdmin();
      
        if(!$authorized)
        {
          $parts = split("[/\\\\]", $path);
      
          // find "clients", compare the following bit to the $session->username
          $clients_pos = array_search("clients", $parts);
          if ($clients_pos && count($parts) >= $clients_pos) 
            $authorized = ($parts[$clients_pos + 1] == $session->username);
          else
            $authorized = false;
        }
        return $authorized;
      }
      

      【讨论】:

        【解决方案3】:

        这样的事情可能会让你开始:

        <?php
        $regex = '%(.+)(/|\\\\)clients(/|\\\\)(.+)(/|\\\\)(.+)(/|\\\\)%';
        
        preg_match($regex, "/volumes/raid0/www/clients/mikey/test_folder/", &$matches);
        var_dump($matches);
        ?>
        

        输出:

         array(8) {
          [0]=>
          string(45) "/volumes/raid0/www/clients/mikey/test_folder/"
          [1]=>
          string(18) "/volumes/raid0/www"
          [2]=>
          string(1) "/"
          [3]=>
          string(1) "/"
          [4]=>
          string(5) "mikey"
          [5]=>
          string(1) "/"
          [6]=>
          string(11) "test_folder"
          [7]=>
          string(1) "/"
        }
        

        应该可以让你把这段代码写得更短。请记住,您必须双重转义。这很丑,我知道。

        【讨论】:

          【解决方案4】:

          您的正则表达式是否也尝试考虑路径中的反斜杠?这可能没有必要。然后你可以拆分:

          '%/clients/%'
          

          (您不需要转义正则斜杠,因为您使用 % 作为正则表达式分隔符。)

          【讨论】:

            【解决方案5】:

            此代码未经测试,适用标准免责声明:

            if($session->isAdmin()) {
               $authorized = true;
            } else {
               // split cwd at the first instance of /clients/
               $dir = preg_split('/(\/|\\)clients(\\|\/)/i', getcwd());
               if(count($dir) == 2) { // if /clients/ was in cwd
                  // check if the second piece of cwd starts with the username.
                  $dir = preg_split('/\/|\\/', $dir[1]);
                  $authorized = ($dir[0] == $session->username);
               } else {
                  $authorized = false;
               }
            }
            

            【讨论】:

            • 这给了我以下错误:
              警告:编译失败:缺少)在第 73 行的 c:\apache\htdocs\clients\mikey\index.php 偏移量 19
              第 73 行是您发布的代码的第 5 行...
            【解决方案6】:

            这修复了我的 PHP 警告:

            if($session->isAdmin())
            {
                $authorized = true;
            } else {
                // split cwd at the first instance of /clients/
                $dir = spliti('%(/|\\\\)clients(/|\\\\)%',getcwd(),2);
                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;
            }
            

            问题是, spliti() 没有拆分我的 cwd() 返回...我正在测试的 cwd() 返回是:

            c:\apache\htdocs\clients\mikey

            我希望能够使用“/”或“\”,这会迫使我使用正则表达式(据我所知)。

            Ruben 的解决方案不太优雅(它似乎限制了允许的目录深度),但如果我想不通,它可能会起作用。

            我会继续努力,看看我能不能弄清楚发生了什么(此时我的 spliti() 正则表达式似乎都是一个问题。)

            输入/反馈总是受到赞赏,即使它不是问题的完整解决方案(甚至相关。如果我的代码很糟糕,请告诉我。)

            谢谢! --威尔(OP)

            【讨论】:

              【解决方案7】:

              好的,解决了。我需要用 preg_split() 替换 split()...

              【讨论】:

                【解决方案8】:

                我会检查 DIRECTORY_SEPARATOR 当前使用的是什么,然后做一个简单的 strpos 检查:

                $cwd = getcwd();
                if (DIRECTORY_SEPARATOR != '/') {
                    $cwd = str_replace(DIRECTORY_SEPARATOR, '/', $cwd);
                }
                $authorized = (strpos($cwd, '/clients/'.$session->username.'/') !== false);
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2015-01-08
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多