Codecolorer - preg_replace_callback()

时间:2015-12-10 16:21:36

标签: php wordpress

我使用Wordpress插件Codecolorer(https://wordpress.org/plugins/codecolorer/),在PHP 7上我有这个问题:

  /** 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;
  }

给我:

[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/

因此我尝试将其更改为:

/** 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;
  }

但现在我收到了这个错误:

[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

请你能帮我解决这个问题吗?谢谢!

1 个答案:

答案 0 :(得分:2)

您实际上应该传递函数名称或可调用对象作为第二个参数。

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

您可以在docs

中详细了解相关内容