zl程序教程

您现在的位置是:首页 >  其他

当前栏目

WP插件CodeColorer兼容PHP7

2023-02-18 16:44:23 时间

本文参考官方文档 PHP7/preg_replace函数 请定位codecolorer代码插件编辑界面并定位codecolorer/codecolorer-core.php文件修改4段代码


修改代码片段 1

$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(.*?)\\>(.*?)\\(\\s*)#sie', '$this->PerformHighlightCodeBlock(\\'\\\\3\\', \\'\\\\2\\', $content, \\'\\', \\'\\\\1\\', \\'\\\\4\\');', $content);

变更为

$content = preg_replace_callback('#(\\s*)\\[cc([^\\s\\]_]*(?:_[^\\s\\]]*)?)([^\\]]*)\\](.*?)\\[/cc\\2\\](\\s*)#si', function($r){
return $this->PerformHighlightCodeBlock($r[4], $r[3], $content, $r[2], $r[1], $r[5]);
}, $content);
$content = preg_replace_callback('#(\\s*)\\<code(.*?)\\>(.*?)\\(\\s*)#si', function($r){
return $this->PerformHighlightCodeBlock($r[3], $r[2], $content, '', $r[1], $r[4]);
}, $content);

修改代码片段 2

$text = preg_replace('~*([0-9a-f]+);~ei', 'chr(hexdec("\\\\1"))', $text);
$text = preg_replace('~*([0-9]+);~e', 'chr(\\\\1)', $text);

变更为

$text = preg_replace_callback('~*([0-9a-f]+);~i', function($r){
return chr(hexdec($r[1]));
}, $text);
$text = preg_replace_callback('~*([0-9]+);~', function($r){
return chr($r[1]);
}, $text);

修改代码片段 3

$content = preg_replace('#(\\s*)(\\[cc[^\\s\\]_]*(?:_[^\\s\\]]*)?[^\\]]*\\].*?\\[/cc\\1\\])(\\s*)#sie', '$this->PerformProtectComment(\\'\\\\2\\', $content, \\'\\\\1\\', \\'\\\\3\\');', $content);
$content = preg_replace('#(\\s*)(\\<code.*?\\>.*?\\)(\\s*)#sie', '$this->PerformProtectComment(\\'\\\\2\\', $content, \\'\\\\1\\', \\'\\\\3\\');', $content);

变更为

$content = preg_replace_callback('#(\\s*)(\\[cc[^\\s\\]_]*(?:_[^\\s\\]]*)?[^\\]]*\\].*?\\[/cc\\1\\])(\\s*)#si', function($r){
return $this->PerformProtectComment($r[2], $content, $r[1], $r[3]);
}, $content);
$content = preg_replace_callback('#(\\s*)(\\<code.*?\\>.*?\\)(\\s*)#si', function($r){
return $this->PerformProtectComment($r[2], $content, $r[1], $r[3]);
}, $content);

修改代码片段 4

$text = preg_replace('/(&lt; \\?php)/i', '<!--?php', $text);&lt;br ?--> $text = preg_replace('/(?:^(?:\\s*[\\r\\n])+|\\s+$)/', '', $text);

变更为

$text = preg_replace_callback('/(&lt; \\?php)/i', function($r){
return '<!--?php'; &lt;br ?--> }, $text);
$text = preg_replace_callback('/(?:^(?:\\s*[\\r\\n])+|\\s+$)/', function($r){
return '';
}, $text);