Fork me on GitHub

Comment by Otomatic (2013-04-12 14:40:29)

Edited by Otomatic (2013-04-12 14:43:43)

 1:  1:
 +I think FluxBB use this "feature" because it is easier to use preg_replace mid modifier "e" than preg_replace_callback that does not accept array as callback function.
 +
 Personally, I prefer the alternative that performs only very few changes in the scripts where it will need to replace "preg_replace" with "preg_replace_callback," and only if you are using PHP 5.5.x. Personally, I prefer the alternative that performs only very few changes in the scripts where it will need to replace "preg_replace" with "preg_replace_callback," and only if you are using PHP 5.5.x.
  
 I add two new functions in the file [i]include/functions.php[/i]: I add two new functions in the file [i]include/functions.php[/i]:
 [code]  [code]
 //[modif oto] for PHP 5.5.0 - Replacement of preg_replace and pattern modifier "e" with preg_replace_callback  //[modif oto] for PHP 5.5.0 - Replacement of preg_replace and pattern modifier "e" with preg_replace_callback
 function php55_callback($element)  function php55_callback($element)
 {  {
  return str_replace(   return str_replace(
  array('\'$1$3\'','\'$2$4\'','\'$1\'', '\'$2\'', '$1', '$2'),   array('\'$1$3\'','\'$2$4\'','\'$1\'', '\'$2\'', '$1', '$2'),
  array('$matches[1].$matches[3]','$matches[2].$matches[4]','$matches[1]','$matches[2]','$matches[1]','$matches[2]'),   array('$matches[1].$matches[3]','$matches[2].$matches[4]','$matches[1]','$matches[2]','$matches[1]','$matches[2]'),
  $element);   $element);
 }  }
   
 //Separate patterns with "e" modifier and without  //Separate patterns with "e" modifier and without
 function php55_preg_replace($pattern, $replace, $text)  function php55_preg_replace($pattern, $replace, $text)
 {  {
  //if not PHP 5.5.x no changes   //if not PHP 5.5.x no changes
  if(version_compare(PHP_VERSION, '5.5.0', '<'))   if(version_compare(PHP_VERSION, '5.5.0', '<'))
  return preg_replace($pattern, $replace, $text);   return preg_replace($pattern, $replace, $text);
   
  if(!is_array($pattern)) {   if(!is_array($pattern)) {
  $pattern = array($pattern);   $pattern = array($pattern);
  $replace = array($replace);   $replace = array($replace);
  }   }
  $count = count($pattern);   $count = count($pattern);
  $pattern_normal = $replace_normal = array();   $pattern_normal = $replace_normal = array();
  for($i = 0; $i < $count ; $i++)   for($i = 0; $i < $count ; $i++)
  {   {
  if(substr($pattern[$i],-1) == "e")   if(substr($pattern[$i],-1) == "e")
  $text = preg_replace_callback(substr($pattern[$i],0,-1), create_function('$matches','return '.php55_callback($replace[$i]).';'), $text);   $text = preg_replace_callback(substr($pattern[$i],0,-1), create_function('$matches','return '.php55_callback($replace[$i]).';'), $text);
  else   else
  {   {
  $pattern_normal[] = $pattern[$i];   $pattern_normal[] = $pattern[$i];
  $replace_normal[] = $replace[$i];   $replace_normal[] = $replace[$i];
  }   }
  }   }
  return preg_replace($pattern_normal, $replace_normal, $text);   return preg_replace($pattern_normal, $replace_normal, $text);
 }  }
 [/code] [/code]
 Then it will only be necessary to change only the calls "preg_replace" who need it to "php55_preg_replace." Then it will only be necessary to change only the calls "preg_replace" who need it to "php55_preg_replace."