0
votes

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
Can you show us how rowtemplate looks like just before the replace? - Matteo Tassinari
its set just before as protected $rowtemplate = ""; - MadDorris
@Jack - after reading the manual i was not able to make this. And this is a point of my topic. I'm not a developer, sorry for this... - MadDorris
Surely you must have tried something; the manual page has three examples that you can play with ... not being a developer doesn't mean "just post a 'give meh teh codez' question" - Ja͢ck
I have played with many examples from here and from official PHP site the last 1 hour with no success before asking there. I'm not the one who ask before try. - MadDorris

1 Answers

2
votes

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