Heim > Artikel > Backend-Entwicklung > php 下面的函数怎么改?
<code>protected function doCodeBlocks($text) { # # Process Markdown `<code>` 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 = "<pre class='\"prettyprint\"'>$codeblock\n"; return $this->hashBlock($codeblock); }
这第一个个函数中的参数$text是markdown格式文章中的代码内容,markdown 格式的内容是这种形式的
<code>{{{ 代码 }}} </code>
如果markdown格式的代码块是这种形式的 {{{javascript: 代码}}}
我想把javascript:
用变量$lang_code的形式提交给$codeblock里面,之后返回下面的html内容:
<code>$codeblock = "<pre class="brush:php;toolbar:false"><code class='\"$lang_code\"'>$codeblock\n</code>";
<code>protected function doCodeBlocks($text) { # # Process Markdown `<code>` 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 = "<pre class='\"prettyprint\"'>$codeblock\n"; return $this->hashBlock($codeblock); }
这第一个个函数中的参数$text是markdown格式文章中的代码内容,markdown 格式的内容是这种形式的
<code>{{{ 代码 }}} </code>
如果markdown格式的代码块是这种形式的 {{{javascript: 代码}}}
我想把javascript:
用变量$lang_code的形式提交给$codeblock里面,之后返回下面的html内容:
<code>$codeblock = "<pre class="brush:php;toolbar:false"><code class='\"$lang_code\"'>$codeblock\n</code>";
在这个后面
<code># trim leading newlines and trailing newlines $codeblock = preg_replace('/\A\n+|\n+\z/', '', $codeblock); </code>
加上下面这两行试试:
<code>$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; } </code>