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 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等