需要更改php5.6的preg_replalce [重复]

guykilcj  于 2023-01-12  发布在  PHP
关注(0)|答案(1)|浏览(126)
    • 此问题在此处已有答案**:

Replace preg_replace() e modifier with preg_replace_callback(3个答案)
两年前关闭了。
在我的PHP程序中,我在将以下代码段中的preg_replace更改为preg_replace_callback时遇到了问题。

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;
}

在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

代码行726是:

$johnny_left_teat .= preg_replace($find, $repl, $tpl);
wfveoks0

wfveoks01#

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;}

相关问题