簡體   English   中英

如何比較字符串並忽略 PHP 中的斜杠和反斜杠

[英]How to compare strings and ignore slashes and backslashes in 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>';
            }
        }
}
    (...)
$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>';
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM