2010-09-29 55 views
1
if (preg_match('^'.preg_quote($this->config_document_root), $filename)) { 

    $AbsoluteFilename = $filename; 

    $this->DebugMessage('ResolveFilenameToAbsolute() NOT prepending $this->config_document_root ('.$this->config_document_root.') to $filename ('.$filename.') resulting in ($AbsoluteFilename = "'.$AbsoluteFilename.'")', __FILE__, __LINE__); 

    } else { 

    $AbsoluteFilename = $this->config_document_root.$filename; 

    $this->DebugMessage('ResolveFilenameToAbsolute() prepending $this->config_document_root ('.$this->config_document_root.') to $filename ('.$filename.') resulting in ($AbsoluteFilename = "'.$AbsoluteFilename.'")', __FILE__, __LINE__); 

    } 
} 

此代碼已經解決了第一個答案的說明,但我該如何修復此代碼?如何在此代碼中將de eregi更改爲preg_match?

if (!$this->config_allow_src_above_docroot && !preg_match('^'.preg_quote(str_replace(DIRECTORY_SEPARATOR, '/', realpath($this->config_document_root))), $AbsoluteFilename)) { 

解決了,感謝所有的答案!

+0

@ user461672我已經用第二個代碼提取的例子更新了答案。另外,在Stackoverflow上發佈代碼時,您應該選擇它並使用'101010'按鈕(或按Ctrl + K)將其格式化爲代碼。 – mikej 2010-09-29 12:20:33

回答

0

這個問題有點令人困惑,因爲你實際上並沒有在發佈的代碼中使用eregi

但是,如果您想檢查$filename是否以$this->config_document_root開頭,則不需要正則表達式。例如爲什麼不使用strpos

if (strpos($filename, $this->config_document_root) === 0) { 
... 

在正則表達式的^被錨定模式文本的開始相匹配,使得如果模式的剩下的只是簡單的文本,然後這實際上只是一個開始與檢查,您的第二個例子可以寫成:

$docroot = str_replace(DIRECTORY_SEPARATOR, '/', realpath($this->config_document_root)); 
if (!$this->config_allow_src_above_docroot && strpos($filename, $docroot) !== 0) { 
... 
+0

我已經使用'strpos'作爲第二種情況的例子更新了答案。你現在刪除了你的評論?你有沒有想過自己? – mikej 2010-09-29 12:13:57

+0

不,這是我在WordPress的博客的代碼,thumnnails沒有顯示在主頁上,現在錯誤在這個文件中,我修復了其他文件中的其他錯誤,但是我的php很弱。感謝所有。 – Rendson 2010-09-29 12:48:37

0

你已經在代碼引用preg_match,但我可以看到它不會工作。我認爲這是第一行開始作爲eregi(),你需要幫助?

如果是這樣的話,你需要進行如下更改:

  • 首先,匹配字符串需要開始與一個正則表達式標記字符(通常/用於此)結束。
  • 其次,既然您指定了eregi(),您還需要在preg_match中添加一個i修飾符,以使其不區分大小寫。

所以給予eregi()的表達,看起來像這樣:

'^matchthis' 

您需要將其更改爲:

'/^matchthis/i' 

顯然與匹配字符串替換matchthis(即你的例子中的preg_quote($this->config_document_root))。

我在前幾天寫了一篇關於ereg和preg在PHP中的區別的a detailed explaination。你可能會發現有用的閱讀。

但是,在您的示例中,您要檢查的是該字符串以變量$this->config_document_root的內容開頭。假設$this->config_document_root本身不包含任何正則表達式模式(並且使用preg_quote幾乎可以保證它不會),但實際上並不需要使用正則表達式 - 您可以使用strpos()或其他幾種常規PHP之一字符串函數。這樣做效率會更高。