doulan1866 2015-01-13 09:55
浏览 24
已采纳

不推荐使用:Joomla插件中的preg_replace()

i have a problem with one 3rd party component for Joomla 3. Unfortunately i'm not an advanced php developer and the component owner does not support this for now, so i'm completely on my own =)

In advance - i have read all related topics there and was not able to make it ont he right way.

My problem is to convert this line:

return preg_replace('/\{([a-zA-Z_]+)\}/e', '$item->\\1', $this->rowtemplate);

with preg_replace_callback(), since in php 5.5 /e parameter is deprecated.

Thanks a lot in advance.

Edit:

There is the whole code part:

public function loadRowtemplate ($item)
{
    $table = $this->params->get('table');


    if(!$this->rowtemplate) {
        $rowtemplate = $table['row'][0] ? "<td><p>" . nl2br($table['row'][0]) . "</p></td>" : "";
        $rowtemplate .= $table['row'][1] ? "<td><p>" . nl2br($table['row'][1]) . "</p></td>" : "";
        $rowtemplate .= $table['row'][2] ? "<td><p>" . nl2br($table['row'][2]) . "</p></td>" : "";
        $rowtemplate .= $table['row'][3] ? "<td><p>" . nl2br($table['row'][3]) . "</p></td>" : "";
        $rowtemplate .= $table['row'][4] ? "<td><p>" . nl2br($table['row'][4]) . "</p></td>" : "";
        $this->rowtemplate = str_replace(",", "<br/>", $rowtemplate);
    }

    **return preg_replace('/\{([a-zA-Z_]+)\}/e', '$item->\\1', $this->rowtemplate);**

}

Edit 2:

There is correct working solution for Joomla 3 and Profiler by Harold Prins Extension (com_profiler) with PHP 5.5:

return preg_replace_callback(
'/\{([a-zA-Z_]+)\}/',
function ($match) use ($item) {
    if (isset($item->{$match[1]})) {
        return $item->{$match[1]};
    }

    return "";
},
$this->rowtemplate

);

Thanks a lot to Matteo Tassinari for solution.

  • 写回答

1条回答 默认 最新

  • duanshan1511 2015-01-13 09:58
    关注

    What you want should look like:

    return preg_replace_callback(
        '/\{([a-zA-Z_]+)\}/',
        function ($match) use ($item) {
            if (isset($item->{$match[1]})) {
                return $item->{$match[1]};
            }
    
            return "";
        },
        $this->rowtemplate
    );
    

    see also the docs for the function itself: http://php.net/manual/en/function.preg-replace-callback.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题