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

Php 不推荐使用:Joomla插件中的preg_replace(),php,preg-replace,joomla3.0,joomla-extensions,Php,Preg Replace,Joomla3.0,Joomla Extensions,我对Joomla 3的一个第三方组件有问题。不幸的是,我不是一名高级php开发人员,组件所有者目前不支持此功能,因此我完全依靠自己=) 提前-我已经阅读了那里所有相关的主题,但没有找到正确的方法 我的问题是转换此行: return preg_replace('/\{([a-zA-Z_]+)\}/e', '$item->\\1', $this->rowtemplate); 使用preg_replace_callback(),因为在PHP5.5/e中不推荐使用参数 先谢谢你 编辑: 这

我对Joomla 3的一个第三方组件有问题。不幸的是,我不是一名高级php开发人员,组件所有者目前不支持此功能,因此我完全依靠自己=)

提前-我已经阅读了那里所有相关的主题,但没有找到正确的方法

我的问题是转换此行:

return preg_replace('/\{([a-zA-Z_]+)\}/e', '$item->\\1', $this->rowtemplate);
使用preg_replace_callback(),因为在PHP5.5/e中不推荐使用参数

先谢谢你

编辑:

这是整个代码部分:

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);**
))


非常感谢Matteo Tassinari提供的解决方案。

您想要的应该是:

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

        return "";
    },
    $this->rowtemplate
);

另请参阅函数本身的文档:

您好,非常感谢您这么快的回答。我也使用了类似的解决方案,但这一个,也和我的一样,给了我一个关于未定义变量的通知,根据$match…相同的错误。但我认为这不是关于$item,而是关于$match@MadDorris实际的通知消息是什么?抱歉,但仍然是一个通知:未定义的变量通知:未定义的变量:C:\localhost\www\xxx\xxx\components\com\u profiler\views\users\view.html.php中的名称,第79行第79行:return$item->${$m[1]};您能告诉我们rowtemplate在替换之前的样子吗?它在替换之前设置为受保护$rowtemplate=“”@杰克-读完手册后,我没能做到这一点。这是我的主题。我不是开发人员,对此感到抱歉……你肯定试过什么东西;手册页面有三个示例,您可以使用它们。。。不是开发人员并不意味着“只需发布一个‘give meh teh codez’问题”,我在这里和官方PHP网站上玩了很多例子,但在提问之前的最后一个小时都没有成功。我不是那个在尝试之前先问的人。
return preg_replace_callback(
    '/\{([a-zA-Z_]+)\}/',
    function ($match) use ($item) {
        if (isset($item->{$match[1]})) {
            return $item->{$match[1]};
        }

        return "";
    },
    $this->rowtemplate
);