gpt4 book ai didi

php - 代码着色器 - preg_replace_callback()

转载 作者:搜寻专家 更新时间:2023-10-31 21:04:56 25 4
gpt4 key购买 nike

我使用 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

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

最佳答案

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

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

您可以在 docs 中阅读更多相关信息.

关于php - 代码着色器 - preg_replace_callback(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34206806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com