Skip to content
Advertisement

How to pass the parameters properly for a callback function for preg_replace_callback using a preexisting function?

I am getting this error: Parse error: syntax error, unexpected ” (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)……… eval()’d code on line 2 Since removing e from the regex expression ~<IF (.?)(?<!-)>(.?)~se as shown in the line below because it became deprecated.

$template['template_html'] = preg_replace('~<IF (.*?)(?<!-)>(.*?)</IF>~se', '$this->get_templates_callback('\1', '\2', $template['template_name'])', $template['template_html']);

I understood I need to use preg_replace_callback() and rewrite using an anonymous function however I am running into troubles rewriting it as $this->get_templates_callback() needs to be called and executed as well.

    function get_templates_callback($condition, $code, $piece)
{
    $macro_id = isset($this->macro[$piece]) ? count($this->macro[$piece]) : 0;
    $this->macro[$piece][$macro_id] = '$macro_replace[' . $macro_id . '] = ((' . $condition . ') ? "' . $code . '" : ""); ';
    return '{' . chr(36) . 'macro_replace[' . $macro_id . ']}';
}

How should I properly go about properly passing parameters into this function?

Advertisement

Answer

You can use use statement to pass additional parameters.

function (array $matches) use ($condition, $code, $piece)
{
}
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement