【问题标题】:Deprecated: preg_replace() in Joomla Plugin已弃用:Joomla 插件中的 preg_replace()
【发布时间】:2015-01-13 09:55:06
【问题描述】:

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

提前 - 我已经阅读了那里的所有相关主题,但无法以正确的方式完成。

我的问题是转换这一行:

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

使用 preg_replace_callback(),因为在 php 5.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);**

}

编辑 2:

Harold Prins Extension (com_profiler) 使用 PHP 5.5 为 Joomla 3 和 Profiler 提供了正确的工作解决方案:

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

    return "";
},
$this->rowtemplate

);

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

【问题讨论】:

  • 你能告诉我们rowtemplate在替换之前的样子吗?
  • 它之前的设置为 protected $rowtemplate = "";
  • @Jack - 阅读手册后我无法做到这一点。这是我的主题。我不是开发人员,对此感到抱歉...
  • 你肯定尝试过什么;手册页提供了三个您可以使用的示例......不是开发人员并不意味着“只发布一个'give meh teh codez'问题”
  • 在过去的 1 小时里,我从这里和官方 PHP 网站上玩了很多例子,但在问之前没有成功。我不是在尝试之前先问的人。

标签: php preg-replace joomla3.0 joomla-extensions


【解决方案1】:

你想要的应该是这样的:

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

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

另请参阅函数本身的文档:http://php.net/manual/en/function.preg-replace-callback.php

【讨论】:

  • 您好,非常感谢您这么快的回答。我也在使用类似的解决方案,但是这个也和我的一样,根据 $match 向我抛出一个关于未定义变量的通知...
  • 同样的错误。但我认为这不是关于 $item 而是关于 $match
  • @MadDorris 实际的通知消息是什么?
  • 对不起,但仍然是一个通知:未定义的变量
  • 注意:未定义的变量:C:\localhost\www\xxx\xxx\components\com_profiler\views\users\view.html.php 中的名称在第 79 行第 79 行:返回 $item-> ${$m[1]};
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-29
  • 2014-02-19
  • 2014-04-13
  • 2013-10-15
  • 1970-01-01
  • 2011-12-06
相关资源
最近更新 更多