Home > Backend Development > PHP Tutorial > php 下面的函数怎么改?

php 下面的函数怎么改?

WBOY
Release: 2016-06-06 20:39:02
Original
948 people have browsed it

<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
Copy after login
Copy after login
"; return $this->hashBlock($codeblock); }

这第一个个函数中的参数$text是markdown格式文章中的代码内容,markdown 格式的内容是这种形式的

<code>{{{ 代码 }}}
</code>
Copy after login
Copy after login

如果markdown格式的代码块是这种形式的 {{{javascript: 代码}}}
我想把javascript:用变量$lang_code的形式提交给$codeblock里面,之后返回下面的html内容:

<code>$codeblock = "<pre class="brush:php;toolbar:false"><code class='\"$lang_code\"'>$codeblock\n</code>
Copy after login
Copy after login
";

回复内容:

<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
Copy after login
Copy after login
"; return $this->hashBlock($codeblock); }

这第一个个函数中的参数$text是markdown格式文章中的代码内容,markdown 格式的内容是这种形式的

<code>{{{ 代码 }}}
</code>
Copy after login
Copy after login

如果markdown格式的代码块是这种形式的 {{{javascript: 代码}}}
我想把javascript:用变量$lang_code的形式提交给$codeblock里面,之后返回下面的html内容:

<code>$codeblock = "<pre class="brush:php;toolbar:false"><code class='\"$lang_code\"'>$codeblock\n</code>
Copy after login
Copy after login
";

在这个后面

<code># trim leading newlines and trailing newlines
$codeblock = preg_replace('/\A\n+|\n+\z/', '', $codeblock);
</code>
Copy after login

加上下面这两行试试:

<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>
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template