Yes, I know I'm a PHP developer. But some problems are tougher than others, and I'd prefer to have help if someone's already solved this problem.
My problem:
Updating preg_replace (with the /e modifier) with preg_replace_callback.
http://stackoverflow.com/questions/15454220/replace-preg-replace-e-modifier-with-preg-replace-callbackMy new server, as well as my local Linux server, both use a very recent version of PHP that is higher than v5.5. That means preg_replace with "/e" is deprecated -- I can't use it anymore.
Regarding the conversion that needs to be done, I understand SOME of it, maybe even MUCH of it, but I'm still having a problem with certain instances of this switchover.
I found this code, which is SUPPOSED to be a drop-in replacement for all existing cases, but it's not perfect I guess.
// for PHP 5.5.0 - Replacement of preg_replace and pattern modifier "e" with preg_replace_callback
function php55_callback($element)
{
return str_replace(
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]'),
$element);
}
//Separate patterns with "e" modifier and without
function php55_preg_replace($pattern, $replace, $text)
{
//if not PHP 5.5.x no changes
if(version_compare(PHP_VERSION, '5.5.0', '<'))
return preg_replace($pattern, $replace, $text);
if(!is_array($pattern)) {
$pattern = array($pattern);
$replace = array($replace);
}
$count = count($pattern);
$pattern_normal = $replace_normal = array();
for($i = 0; $i < $count ; $i++)
{
if(substr($pattern[$i],-1) == "e")
$text = preg_replace_callback(substr($pattern[$i],0,-1), create_function('$matches','return '.php55_callback($replace[$i]).';'), $text);
else
{
$pattern_normal[] = $pattern[$i];
$replace_normal[] = $replace[$i];
}
}
return preg_replace($pattern_normal, $replace_normal, $text);
}
Here is the code where the problem is:
(You'll have to "quote" my post and get the actual code out; I can't stop the board from formatting for smilies, formatting, etc. That's why a bunch of the code here has "strikethrough"...)
if (($options & FORMAT_MBCODE) && $brackets) {
$search = array(
'~[(/)?([bi])]~i',
'~
~i',
'~~i',
'~[/[us]]~i',
'~http://(h t t p|h t t p s|f t p) : / /(.+?)~ise',
'~(.+?)~ise',
'~([a-z0-9-_.]+@[a-z0-9-.]+.[a-z0-9-_.]+)?~i',
'~+?)](.*?)~i',
'~~ise',
'~[youtube](h t t p|h t t p s|f t p) : / /(.*?)[/youtube]~ise',
'~[(right|center)](.*?)[/1]~is',
'~(.*?)
~ise',
'~
(.*?)~ise',
'~[php=([0-9]+?)](.*?)[/php]~ise',
'~[color=([A-Za-z ]+?)](.*?)[/color]~is',
'~[font=([A-Za-z ]+?)](.*?)[/font]~is',
'~[size=([1-9]+?)](.*?)[/size]~is'
);
// <iframe width="432" height="243" src="
https://www.youtube.com/embed/usHOxIkanHY?rel=0" frameborder="0" allowfullscreen></iframe>
$replace = array(
'<12>',
'<span style='text-decoration:underline'>',
'<span style='text-decoration:line-through'>',
'</span>',
''<a href="' . str_replace(' ', '', '1://2') . '" onclick="window.open(this.href,'' . $this->sets['link_target'] . '');return false;">' . str_replace(' ', '', '1://2') . '[/url]'',
''<a href="' . str_replace(' ', '', '1://2') . '" onclick="window.open(this.href,'' . $this->sets['link_target'] . '');return false;">' . str_replace('"', '"', '3') . '[/url]'',
'
1',
'
2',
''<img src="' . str_replace(' ', '', '1://2') . '" width="500" alt="' . str_replace(' ', '', '1://2') . '" />'',
''<iframe src="' . str_replace(' ', '', '1://2') . '" width="500" height="315" alt="' . str_replace(' ', '', '1://2') . '" frameborder="0" allowfullscreen></iframe>'',
'<div align="1">2</div>',
'$this->format_code('1', 0)',
'$this->format_code('1', 1)',
'$this->format_code('2', 1, '1')',
'<span style='color:1'>2</span>',
'<span style='font-family:1'>2</span>',
'<span style='font-size:1ex'>2</span>'
);
if ((substr_count($in, '
') + substr_count($in, '')) {
$search[] = '~~i';
$search[] = '~~i';
$search[] = '~
~i';
$replace[] = '<table style="width:90%; margin-left:5%; margin-right:5%;" border="0" cellpadding="3" cellspacing="0"><tr><td>1 ' . $this->lang->main_said . ':</td></tr><tr><td class="quote">';
$replace[] = '<table style="width:90%; margin-left:5%; margin-right:5%;" border="0" cellpadding="3" cellspacing="0"><tr><td>' . $this->lang->main_quote . ':</td></tr><tr><td class="quote">';
$replace[] = '</td></tr></table>';
}
$in = $this->php55_preg_replace($search, $replace, $in);