PHP 5.5 and preg_replace function depracation

edited November 2013 in Bug Reports
I don't suppose anyone has run into this bug using XAMPP with PHP 5.5 concerning the use of the preg_replace function. The code below comes to us from the Data_table file located in fuel/modules/fuel/libraries on or around line 913 in an editor.

$url = preg_replace('#^(.*)\{(.+)\}(.*)$#e', "'\\1'.((!empty(\$fields['\\2'])) ? \$fields['\\2'] : '').'\\3'", $val['url']);

The preg_replace function is deprecated and calls for the use of the preg_replace_callback function. Now I've never had much luck in writing regular expressions, much less updating one. I thought there might be an updated Data_table file on Github, but no such luck. Has anyone managed to get around the below error?


A PHP Error was encountered

Severity: 8192

Message: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead

Filename: libraries/Data_table.php

Line Number: 913

Comments

  • edited 12:03PM
    Not yet. The e modifier is used in a few spots and those areas need to be updated to use the callback instead. We will have this before the 1.0 release but until then you'll have to either use 5.4 or implement the fix. If you do the latter, let us know or better yet, put it in a pull request.
    https://github.com/daylightstudio/FUEL-CMS/tree/1.0
  • edited November 2013
    What if you try replacing that line with something like the following:
    //e modifier is deprecated if (version_compare(PHP_VERSION, '5.5', '>=')) { $url = preg_replace_callback('#^(.*)\{(.+)\}(.*)$#', function($match) use ($fields) { if (!empty($match[2])) { return $match[1].$fields[$match[2]].$match[3]; } return ''; }, $val['url']); } else { $url = preg_replace('#^(.*)\{(.+)\}(.*)$#e', "'\\1'.((!empty(\$fields['\\2'])) ? \$fields['\\2'] : '').'\\3'", $val['url']); }
  • edited 12:03PM
    That would have taken me a month to write, but I will try that and report back. Thanks brother :-)
  • edited 12:03PM
    Wow, that worked:-) Thanks again admin. I've pasted the code for this particular function below in its entirety.

    protected function _render_actions($actions, $fields)
    {
    $str = '';
    $actions = array();
    foreach($this->_actions as $key => $val)
    {
    // normalize the data
    if (!is_array($val))
    {
    $val = array('url' => $val);
    }
    if (!empty($val['func']))
    {
    $action = call_user_func($val['func'], $fields);
    if (!empty($action)) $actions[] = $action;
    }
    else
    {
    // i love regexp... key is the e modifier that evaluates the code
    //e modifier is deprecated
    if (version_compare(PHP_VERSION, '5.5', '>='))
    {
    $url = preg_replace_callback('#^(.*)\{(.+)\}(.*)$#',
    function($match) use ($fields) {
    if (!empty($match[2]))
    {
    return $match[1].$fields[$match[2]].$match[3];
    }
    return '';
    }, $val['url']);
    }
    else
    {
    $url = preg_replace('#^(.*)\{(.+)\}(.*)$#e', "'\\1'.((!empty(\$fields['\\2'])) ? \$fields['\\2'] : '').'\\3'", $val['url']);
    }
    $attrs = (!empty($val['attrs'])) ? ' '.$this->_render_attrs($val['attrs']) : '';
    $actions[] =''.$key.'';
    }

    }
    if (!empty($actions)) $str = implode('  '.$this->action_delimiter.'  ', $actions);
    return $str;
    }

    }
  • edited 12:03PM
    Also, this change has been pushed into the 1.0 branch.
  • edited 12:03PM
    So what would be this same fix for

    Filename: libraries/fuel_page.php
    Line Number: 646

    The code is:

    $output = preg_replace('/(<body[^>]*>)/e', '"\\1\n".\$head_markers', $output);
  • edited 12:03PM
    In the newest version of FUEL, look at the Fuel_pages::render_all_markers() method.
Sign In or Register to comment.