0

I am working on migrating a php website and getting the following statement

"preg_replace(): The /e modifier is no longer supported, use preg_replace_callback".

When changing to preg_replace_callback, I get an error message "preg_replace_callback(): Requires argument 2, '$this->('\1')', to be a valid callback".

Code:

function getFile($filename)
{
    if ($filename{0} == '/' && substr($this->fileRoot, -1) == '/') {
        $filename = substr($filename, 1);
    }

    $filename = $this->fileRoot . $filename;

    if (!($fh = @fopen($filename, 'r'))) {
        $this->err[] = PEAR::raiseError(
            $this->errorMessage(IT_TPL_NOT_FOUND) . ': "' .$filename .'"',
            IT_TPL_NOT_FOUND
        );
        return "";
    }

    $fsize = filesize($filename);
    if ($fsize < 1) {
        fclose($fh);
        return '';
    }

    $content = fread($fh, $fsize);
    fclose($fh);

    return preg_replace(
        "#<!-- INCLUDE (.*) -->#ime",
        "\$this->$getFile('\\1')",
        $content
    );

} // end func getFile

    /** change from preg_replace to preg_replace_callback */
    return preg_replace_callback(
        "#<!-- INCLUDE (.*) -->#ime",
        "\$this->$getFile('\\1')",
        $content

Thanks in Advance.

Steve Lo
  • 9
  • 2

2 Answers2

0

see this little code to be sure is preg_replace_callback().

 <?php
// this text was used in 2002
// we want to get this up to date for 2003
$text = "April fools day is 04/01/2002\n";
$text.= "Last christmas was 12/24/2001\n";
// the callback function
function next_year($matches)
{
  // as usual: $matches[0] is the complete match
  // $matches[1] the match for the first subpattern
  // enclosed in '(...)' and so on
  return $matches[1].($matches[2]+1);
}
echo preg_replace_callback(
            "|(\d{2}/\d{2}/)(\d{4})|",
            "next_year",
            $text);

?>
YOUNES.EK
  • 76
  • 1
  • 14
0

First as you error message show the variable $this->$getfile does not exist anywhere.You must review this part of the code.

Second Based on your code and your usage of $this we can deduce that your function is a non-static method.So in your case a valid callback could be an array of object(as the first entry) and a method(as the second entry) In your case the array should be [$this,"name_of_your_method_here"]

Take a look at callbacks for more informations.

Elementary
  • 1,443
  • 1
  • 7
  • 17