2015-09-05 168 views
-1

我想比较两个字符串并忽略斜杠和反斜杠。如何在PHP中比较字符串并忽略斜杠和反斜杠

例如,我的预计业绩:

Regex found: tools/dbgprint/dbgprint.c 
Trimmed from $regex->key(): base/applications\cmdutils\dbgprint\dbgprint.c 
Output echo: Different!! 

Regex found: base/applications/cmdutils/help/help.c 
Trimmed from $regex->key(): base/applications\cmdutils\help\help.c 
Output echo: This same 

Regex found: dll\app\mplay32\mplay32. 
Trimmed from $regex->key(): dll\app\mplay32\mplay32.c 
This same 

这是我的代码(输出不等于上面提到的,但我想你明白这一点):

$ROSDir = 'E:/ReactOS/';  
(...)  
$re = "/^\\s*\\*\\sFILE:\\s*\\K(\\S*)/m"; 

(...) 
    if (!$regex->isDot()) 
    { 
     $fileContent = file_get_contents($regex->key()); 

     preg_match_all($re, $fileContent, $matches); 

     if (isset($matches[0][0])) 
     { 
      echo 'File: <b>'. $regex->key() .'</b><br>'; 

      echo 'Re found: '. $matches[0][0] .'<br>'; 

      $subject = $regex->key(); 
      $trimmed = str_replace($ROSDir, '', $subject); 
      echo 'Trimmed: '. $trimmed .'<br>'; 

      if ($matches[0][0] !== $trimmed) 
      { 
       echo 'Different!!<br>'; 
      } 
     } 
} 
    (...) 
+0

什么都有你尝试过,并在特定的地方卡住了?否则,我们需要猜测...... – PeeHaa

+0

将所有斜杠替换为反斜杠(反之亦然)并进行比较。 –

+0

编辑@PeeHaa – Saibamen

回答

0
$slash = str_replace('\\', '/', $trimmed); 

      $backslash = str_replace('/', '\\', $trimmed); 

      if ($matches[0][0] !== $slash && $matches[0][0] !== $backslash) 
      { 
       echo 'File: <b>'. $regex->key() .'</b><br>'; 

       echo 'Re found: '. $matches[0][0] .'<br>'; 

       echo 'Trimmed: '. $trimmed .'<br>'; 

       echo 'Different!!<br>'; 
       $diffHeader++; 

       echo '<hr>'; 
      }