2017-03-08 44 views
0

我交換了一個幾年前的程序,並更新了PHP和MySQL的deprication,並且在preg_matchereg之間的語法上出現堵塞。我嘗試在各處放置斜槓,並且無法提供正確的語法。我錯過了什麼?Preg_match上的語法

舊線:

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

新線:

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

請原諒我福利局的煩躁,我需要逃脫 '/'?

回答

1

你缺少所需的preg_*功能分隔符:

preg_match('#^'.preg_quote(str_replace($this->osslash, '/', realpath($this->config_document_root))) . '#', $AbsoluteFilename) 
      ^                      ^

由於我使用的是#,沒有必要逃避正斜槓。

+0

如果這不起作用呢?如果(!$ this-> config_allow_src_above_docroot &&!preg_match(〜'^'。preg_quote(str_replace($ this-> osslash,'/',realpath($ this-> config_document_root)))〜,$ AbsoluteFilename)){using a波浪?我得到PHP解析錯誤:語法錯誤,意外')' – SL0WLearner

+0

@ SL0WLearner分隔符是正則表達式字符串的一部分,您不能在字符串外添加字符。 – jeroen

+0

還告訴我,我有太多的人。如果(!$ this-> config_allow_src_above_docroot &&!preg_match('〜^'。preg_quote(str_replace($ this-> osslash,'/',realpath($ this-> config_document_root)))。'〜',$ AbsoluteFilename)) { – SL0WLearner