Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codecolorer - preg_replace_callback()

Tags:

php

wordpress

I use Wordpress plugin Codecolorer (https://wordpress.org/plugins/codecolorer/) and on PHP 7 I have this problem:

  /** Search content for code tags and replace it */
  function BeforeHighlightCodeBlock($content) {
    $content = preg_replace('#(\s*)\[cc([^\s\]_]*(?:_[^\s\]]*)?)([^\]]*)\](.*?)\[/cc\2\](\s*)#sie', '$this->PerformHighlightCodeBlock(\'\\4\', \'\\3\', $content, \'\\2\', \'\\1\', \'\\5\');', $content);
    $content = preg_replace('#(\s*)\<code(.*?)\>(.*?)\</code\>(\s*)#sie', '$this->PerformHighlightCodeBlock(\'\\3\', \'\\2\', $content, \'\', \'\\1\', \'\\4\');', $content);

    return $content;
  }

Gives me:

[Thu Dec 10 17:02:36.552179 2015] [:error] [pid 19451] [client 127.0.0.1:48652] PHP Warning:  preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /var/www/html/xxx/wp-content/plugins/codecolorer/codecolorer-core.php on line 49, referer: http://xxx/
[Thu Dec 10 17:02:36.552202 2015] [:error] [pid 19451] [client 127.0.0.1:48652] PHP Warning:  preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /var/www/html/xxx/wp-content/plugins/codecolorer/codecolorer-core.php on line 50, referer: http://xxx/

So I tried to change it to:

/** Search content for code tags and replace it */
  function BeforeHighlightCodeBlock($content) {
    $content = preg_replace_callback('#(\s*)\[cc([^\s\]_]*(?:_[^\s\]]*)?)([^\]]*)\](.*?)\[/cc\2\](\s*)#si', '$this->PerformHighlightCodeBlock(\'\\4\', \'\\3\', $content, \'\\2\', \'\\1\', \'\\5\');', $content);
    $content = preg_replace_callback('#(\s*)\<code(.*?)\>(.*?)\</code\>(\s*)#si', '$this->PerformHighlightCodeBlock(\'\\3\', \'\\2\', $content, \'\', \'\\1\', \'\\4\');', $content);

    return $content;
  }

But now I get this error:

[Thu Dec 10 17:05:52.331876 2015] [:error] [pid 19451] [client 127.0.0.1:48714] PHP Warning:  preg_replace_callback(): Requires argument 2, '$this-&gt;PerformHighlightCodeBlock('\\4', '\\3', $content, '\\2', '\\1', '\\5');', to be a valid callback in /var/www/html/xxx/wp-content/plugins/codecolorer/codecolorer-core.php on line 49
[Thu Dec 10 17:05:52.331910 2015] [:error] [pid 19451] [client 127.0.0.1:48714] PHP Warning:  preg_replace_callback(): Requires argument 2, '$this-&gt;PerformHighlightCodeBlock('\\3', '\\2', $content, '', '\\1', '\\4');', to be a valid callback in /var/www/html/xxx/wp-content/plugins/codecolorer/codecolorer-core.php on line 50

Please can you help me to fix this? Thank you!

like image 731
vaclavambroz Avatar asked May 26 '26 10:05

vaclavambroz


1 Answers

You should actually pass either a function name or a callable object as the second argument.

preg_replace_callback('#(\s*)\[cc([^\s\]_]*(?:_[^\s\]]*)?)([^\]]*)\](.*?)\[/cc\2\](\s*)#si', function($matches){
    // Do something
    return $string; // return some string
}, $content);

You can read more about that in the docs.

like image 186
Alexander Mikhalchenko Avatar answered May 30 '26 08:05

Alexander Mikhalchenko