2015-09-05 112 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>'; 
      }