0

I'm working on porting SocialEngine to work on PHP7.

On the default install I get the following error:

preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in 
<b>/home/vagrant/code/mysite/project/app/application/libraries/Scaffold/modules/NestedSelectors/NestedSelectors.php</b>
on line <b>300</b>

The code for that error looks like so:

$xml = preg_replace('/([-_A-Za-z*]+)\s*:\s*([^;}{]+)(?:;)/ie', "'<property name=\"'.trim('$1').'\" value=\"'.trim('$2').'\" />\n'", $xml);

I replaced it with preg_replace_callback like so:

$xml = preg_replace_callback('/([-_A-Za-z*]+)\s*:\s*([^;}{]+)(?:;)/ie', function($matches) {
    return "'<property name=\"'.trim('$matches[1]').'\" value=\"'.trim('$matches[2]').'\" />\n'";
}, $xml);

and now I get this error:

preg_replace_callback(): The /e modifier is no longer supported, use preg_replace_callback instead in 
<b>/home/vagrant/code/mysite/project/app/application/libraries/Scaffold/modules/NestedSelectors/NestedSelectors.php</b> 
on line <b>304</b>

This seems valid to me, what have I done wrong?

secondman
  • 3,233
  • 6
  • 43
  • 66

2 Answers2

0

instead of

    $xml = preg_replace_callback('/([-_A-Za-z*]+)\s*:\s*([^;}{]+)(?:;)/ie', function($matches) {
    return "'<property name=\"'.trim('$matches[1]').'\" value=\"'.trim('$matches[2]').'\" />\n'";
}, $xml);

do

    $xml = preg_replace_callback('/([-_A-Za-z*]+)\s*:\s*([^;}{]+)(?:;)/i', function($matches) {
    return "'<property name=\"'.trim('$matches[1]').'\" value=\"'.trim('$matches[2]').'\" />\n'";
}, $xml);

What i did was to remove the e in ../ie

Happy coding...

The Billionaire Guy
  • 3,382
  • 28
  • 31
0

Now I don't know anything about anything related to this error or what it means, but here's what I found to fix it:

From https://www.drupal.org/project/views_slideshow/issues/1872616 where they're talking about fixing this magic potion:

I found that there were four places in this file that needed changing. The two already mentioned and then two more times where preg_replace was used. For me they were around lines 186 + 277.

At 186 and 277 I replaced

    $vars['location'] => array(
      'type' => preg_replace('/_(.?)/e',"strtoupper('$1')", $vars['settings']['type']),
    ),

with

    $vars['location'] => array(
      'type' => preg_replace_callback(   // php5.x preg_replace_callback() method
        '/_(.?)/',
        function ($m) {
          return strtoupper($m[1]);
        },
        $vars['settings']['type']),
    ),

So I took the original code:

// Loop through all the addons and call their methods if needed.
foreach ($addons as $addon_id => $addon_info) {
  foreach ($addon_info['accepts'] as $imp_key => $imp_value) {
    if (is_array($imp_value)) {
      $methods[$imp_key][] = preg_replace('/_(.?)/e',"strtoupper('$1')", $addon_id);
    }
    else {
      $methods[$imp_value][] = preg_replace('/_(.?)/e',"strtoupper('$1')", $addon_id);
    }
  }
}

And boiled it and the fix down to these elements:

Original:

preg_replace('/_(.?)/e',"strtoupper('$1')", $vars),),

Fix:

preg_replace_callback('/_(.?)/', function ($m) { return strtoupper($m[1]); }, $vars), ),

And they combine to become:

preg_replace_callback('/_(.?)/', function ($m) { return strtoupper($m[1]); }, $addon_id);

Which, in the context of the theme I'm trying to use, becomes:

// Loop through all the addons and call their methods if needed.
foreach ($addons as $addon_id => $addon_info) {
  foreach ($addon_info['accepts'] as $imp_key => $imp_value) {
    if (is_array($imp_value)) {
      $methods[$imp_key][] = preg_replace_callback('/_(.?)/', function ($m) { return strtoupper($m[1]); }, $addon_id);
    }
    else {
      $methods[$imp_value][] = preg_replace_callback('/_(.?)/',function ($m) { return strtoupper($m[1]); }, $addon_id);
    }
  }
}

I will also say that it was VERY handy that the file location was given in the error, allowing me to fix an otherwise unknowable problem.

seizethecarp
  • 205
  • 2
  • 6