php 下面的函数怎么改?
程序员文章站
2022-06-06 21:22:19
...
protected function doCodeBlocks($text)
{
#
# Process Markdown `` blocks.
#
preg_match('/\{\{\{/i', $text, $_m_c_open);
preg_match('/\}\}\}/i', $text, $_m_c_close);
if (count($_m_c_open) == count($_m_c_close))
{
$text = preg_replace_callback('/\{\{\{[ \n]*(.*?)\}\}\}/is', array(
&$this,
'_doCodeBlocks_callback'
), $text);
}
return $text;
}
protected function _doCodeBlocks_callback($matches) {
$codeblock = $matches[1];
$codeblock = $this->outdent($codeblock);
$codeblock = htmlspecialchars($codeblock, ENT_NOQUOTES);
# trim leading newlines and trailing newlines
$codeblock = preg_replace('/\A\n+|\n+\z/', '', $codeblock);
$codeblock = str_replace(array(
'&',
"\t",
' '
), array(
'&',
' ',
' '
), $codeblock);
$codeblock = "$codeblock\n
";
return $this->hashBlock($codeblock);
}
这第一个个函数中的参数$text是markdown格式文章中的代码内容,markdown 格式的内容是这种形式的
{{{ 代码 }}}
如果markdown格式的代码块是这种形式的 {{{javascript: 代码}}}
我想把javascript:
用变量$lang_code的形式提交给$codeblock里面,之后返回下面的html内容:
$codeblock = "$codeblock\n
";
回复内容:
protected function doCodeBlocks($text)
{
#
# Process Markdown `` blocks.
#
preg_match('/\{\{\{/i', $text, $_m_c_open);
preg_match('/\}\}\}/i', $text, $_m_c_close);
if (count($_m_c_open) == count($_m_c_close))
{
$text = preg_replace_callback('/\{\{\{[ \n]*(.*?)\}\}\}/is', array(
&$this,
'_doCodeBlocks_callback'
), $text);
}
return $text;
}
protected function _doCodeBlocks_callback($matches) {
$codeblock = $matches[1];
$codeblock = $this->outdent($codeblock);
$codeblock = htmlspecialchars($codeblock, ENT_NOQUOTES);
# trim leading newlines and trailing newlines
$codeblock = preg_replace('/\A\n+|\n+\z/', '', $codeblock);
$codeblock = str_replace(array(
'&',
"\t",
' '
), array(
'&',
' ',
' '
), $codeblock);
$codeblock = "$codeblock\n
";
return $this->hashBlock($codeblock);
}
这第一个个函数中的参数$text是markdown格式文章中的代码内容,markdown 格式的内容是这种形式的
{{{ 代码 }}}
如果markdown格式的代码块是这种形式的 {{{javascript: 代码}}}
我想把javascript:
用变量$lang_code的形式提交给$codeblock里面,之后返回下面的html内容:
$codeblock = "$codeblock\n
";
在这个后面
# trim leading newlines and trailing newlines
$codeblock = preg_replace('/\A\n+|\n+\z/', '', $codeblock);
加上下面这两行试试:
$lang_pos = strpos($codeblock,":");
$lang_code_pre = substr($codeblock, 3,$lang_pos - 3);
switch ($lang_code_pre) {
case 'php':
$lang_code = "php";
break;
case 'javascript':
$lang_code = "javascript";
break;
default:
$lang_code = "default value";
break;
}