2

I have problem to change preg_replace to preg_replace_callback in the following piece of code in my PHP program.

    function category_get_tree($prefix = '', $tpl = '{name}', $no_prefix = true, $id = 0, $level = 0){
global $sql, $PHP_SELF;
static $johnny_left_teat;

    $level++;

    foreach ($sql->select(array('table' => 'categories', 'where' => array("parent = $id"), 'orderby' => array('id', 'ASC'))) as $row){
        $find = array('/{id}/i', '/{name}/i', '/{url}/i', '/{icon}/i', '/{template}/i', '/{prefix}/i', '/\[php\](.*?)\[\/php\]/ie');
        $repl = array($row['id'], $row['name'], $row['url'], ($row['icon'] ? '<img src="'.$row['icon'].'" alt="'.$row['name'].'" border="0" align="absmiddle">' : ''), $row['template'], (($row['parent'] or !$no_prefix) ? $prefix : ''), '\\1');
        $johnny_left_teat .= ($no_prefix ? preg_replace('/('.$prefix.'{1})$/i', '', str_repeat($prefix, $level)) : str_repeat($prefix, $level));
        $johnny_left_teat .= preg_replace($find, $repl, $tpl);
        category_get_tree($prefix, $tpl, $no_prefix, $row['id'], $level);
    }

return $johnny_left_teat;
}

The error I receive while running the code in PHP5.6:

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in X:\xampp\htdocs\news\inc\functions.inc.php on line 726

the line 726 of code is:

$johnny_left_teat .= preg_replace($find, $repl, $tpl);
Waterfall
  • 21
  • 2

1 Answers1

-1
function category_get_tree($prefix = '', $tpl = '', $no_prefix = true, $id = 0, $level = 0){
global $sql, $PHP_SELF;
static $johnny_left_teat;
   $level++;
   foreach ($sql->select(array('table' => 'categories', 'where' => array("parent = $id"), 'orderby' => array('ord', 'ASC'))) as $row){

      $find = ['/{(id|name|url|icon|template|prefix)}/', '/\[php\](.*?)\[\/php\]/'];

      $repl = [$row['id'], $row['name'], $row['url'], $row['icon'], $row['template'], $row['template'], ($row['parent'] or !$no_prefix), $prefix];

      $pref = $no_prefix ? $row['level'] : ($row['level'] + 1);
      $pref = $minus ? ($pref - (!$no_prefix ? ($minus - 1) : ($minus - 1))) : $pref;
      $pref = str_repeat($prefix, $pref);
      $row['prefix'] = $pref;

      $johnny_left_teat .= preg_replace_callback($find, function($m) use ($row) {
            return isset($row[$m[1]]) ? $row[$m[1]] : eval('return ' .$m[1]. ';');
        }, $tpl);
      
      category_get_tree($prefix, $tpl, $no_prefix, $row['id'], $level);

} return $johnny_left_teat;}
  • Answer with code only without any explanations is useless. Moreover, this still gets same error – Toto Jun 04 '20 at 12:18