2010-09-29 49 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之一字符串函数。这样做效率会更高。