突出显示javascript的本机php函数?

突出显示javascript的本机php函数?,php,javascript,function,syntax-highlighting,Php,Javascript,Function,Syntax Highlighting,除了javascript,是否有任何本机PHP函数 或者,如果没有,是否有任何PHP函数(自制)来实现它 编辑:我想使用PHP函数为javascript着色是的,PHP函数highlight_string()是PHP的本机PHP函数。否 但是有很多javascript库在几种语言上进行语法突出显示, 从bash脚本到php和javascript 例如,如(JQuery)或(我最喜欢的)以上,您可以找到一个优秀的库,它使用javascripts和css类在大量语言中启用语法高亮显示 没有本机php

除了javascript,是否有任何本机PHP函数

或者,如果没有,是否有任何PHP函数(自制)来实现它


编辑:我想使用PHP函数为javascript着色是的,PHP函数highlight_string()是PHP的本机PHP函数。

但是有很多javascript库在几种语言上进行语法突出显示, 从bash脚本到php和javascript

例如,如(JQuery)或(我最喜欢的)

以上,您可以找到一个优秀的库,它使用javascripts和css类在大量语言中启用语法高亮显示


没有本机php函数可以实现这一点,因此要么使用现有库,要么自己编写一些东西。

我在这方面取得了巨大成功。易于使用并集成到您的应用程序中,它支持多种语言。

我知道您希望使用PHP编写语法更高的语言。这一个(格西)在过去为我工作过:


这里的信息很好。这是另一个不错的方法:

最快的方法-您还可以使用PHP函数“highlight\u string”,并使用一些小技巧 (捕获函数输出并删除前导/尾随PHP标记):

$source=”。。。一些javascript…';
//选项1-纯JS代码
$htmlJs=突出显示字符串(“”,true);
$htmlJs=str_replace(数组(''php','?')、数组('''')、$htmlJs);
//选项2-与JS脚本中的PHP代码混合时
$htmlJs=突出显示字符串('STARTEND',true);
$htmlJs=str_replace(数组('START?php','END')、数组('',')、$htmlJs);
//检查“highlight.keyword”(#0000BB)的PHP INI设置-http://www.php.net/manual/en/misc.configuration.php#ini.syntax-突出显示

没有本机函数,但与其使用完整的堆栈库来突出显示某些javascript,还可以使用以下单个函数:

function format_javascript($data, $options = false, $c_string = "#DD0000", $c_comment = "#FF8000", $c_keyword = "#007700", $c_default = "#0000BB", $c_html = "#0000BB", $flush_on_closing_brace = false)
{

    if (is_array($options)) { // check for alternative usage
        extract($options, EXTR_OVERWRITE); // extract the variables from the array if so
    } else {
        $advanced_optimizations = $options; // otherwise carry on as normal
    }
    @ini_set('highlight.string', $c_string); // Set each colour for each part of the syntax
    @ini_set('highlight.comment', $c_comment); // Suppression has to happen as some hosts deny access to ini_set and there is no way of detecting this
    @ini_set('highlight.keyword', $c_keyword);
    @ini_set('highlight.default', $c_default);
    @ini_set('highlight.html', $c_html);

    if ($advanced_optimizations) { // if the function has been allowed to perform potential (although unlikely) code-destroying or erroneous edits
        $data = preg_replace('/([$a-zA-z09]+) = \((.+)\) \? ([^]*)([ ]+)?\:([ ]+)?([^=\;]*)/', 'if ($2) {' . "\n" . ' $1 = $3; }' . "\n" . 'else {' . "\n" . ' $1 = $5; ' . "\n" . '}', $data); // expand all BASIC ternary statements into full if/elses
    }

    $data = str_replace(array(') { ', ' }', ";", "\r\n"), array(") {\n", "\n}", ";\n", "\n"), $data); // Newlinefy all braces and change Windows linebreaks to Linux (much nicer!)
    $data = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $data); // Regex identifies all extra empty lines produced by the str_replace above. It is quicker to do it like this than deal with a more complicated regular expression above.
    $data = str_replace("<?php", "<script>", highlight_string("<?php \n" . $data . "\n?>", true));

    $data = explode("\n", str_replace(array("<br />"), array("\n"), $data));

# experimental tab level highlighting
    $tab = 0;
    $output = '';

    foreach ($data as $line) {
        $lineecho = $line;
        if (substr_count($line, "\t") != $tab) {
            $lineecho = str_replace("\t", "", trim($lineecho));
            $lineecho = str_repeat("\t", $tab) . $lineecho;
        }
        $tab = $tab + substr_count($line, "{") - substr_count($line, "}");
        if ($flush_on_closing_brace && trim($line) == "}") {
            $output .= '}';
        } else {
            $output .= str_replace(array("{}", "[]"), array("<span style='color:" . $c_string . "!important;'>{}</span>", "<span style='color:" . $c_string . " !important;'>[]</span>"), $lineecho . "\n"); // Main JS specific thing that is not matched in the PHP parser
        }

    }

    $output = str_replace(array('?php', '?&gt;'), array('script type="text/javascript">', '&lt;/script&gt;'), $output); // Add nice and friendly <script> tags around highlighted text

    return '<pre id="code_highlighted">' . $output . "</pre>";
}
学分:
演示:

我想你是说一个突出显示javascript的本机函数?否则,您会回答自己的问题;)是的,你说得对。我知道如何用php给php上色,但我想知道,如何用php给javascript上色:)我相信OP是javascript的意思..检查标题Oho,现在这是一个不同的问题…:)我想找到能够突出显示javascript的PHP函数:)看起来不错,我会试试。然而,我现在正在寻找一个函数,没有一个函数可以做到这一点。突出显示太复杂,无法在单个函数中捕获,也无法理解。如果你找到一个单一的功能,那么它将是一个笨拙的怪物!PHP的
highlight_string()
看起来只是一个函数,因为其余的都隐藏在PHP内部。正如我在演示中看到的,这是一个很棒的工具,但我现在正在搜索单个函数:)我将给你+1 tho
函数highlight($string,$language){include_once'geshi.PHP';$geshi=new geshi($string,$language);return$geshi->parse_code();}
:我正在寻找PHP函数!
function format_javascript($data, $options = false, $c_string = "#DD0000", $c_comment = "#FF8000", $c_keyword = "#007700", $c_default = "#0000BB", $c_html = "#0000BB", $flush_on_closing_brace = false)
{

    if (is_array($options)) { // check for alternative usage
        extract($options, EXTR_OVERWRITE); // extract the variables from the array if so
    } else {
        $advanced_optimizations = $options; // otherwise carry on as normal
    }
    @ini_set('highlight.string', $c_string); // Set each colour for each part of the syntax
    @ini_set('highlight.comment', $c_comment); // Suppression has to happen as some hosts deny access to ini_set and there is no way of detecting this
    @ini_set('highlight.keyword', $c_keyword);
    @ini_set('highlight.default', $c_default);
    @ini_set('highlight.html', $c_html);

    if ($advanced_optimizations) { // if the function has been allowed to perform potential (although unlikely) code-destroying or erroneous edits
        $data = preg_replace('/([$a-zA-z09]+) = \((.+)\) \? ([^]*)([ ]+)?\:([ ]+)?([^=\;]*)/', 'if ($2) {' . "\n" . ' $1 = $3; }' . "\n" . 'else {' . "\n" . ' $1 = $5; ' . "\n" . '}', $data); // expand all BASIC ternary statements into full if/elses
    }

    $data = str_replace(array(') { ', ' }', ";", "\r\n"), array(") {\n", "\n}", ";\n", "\n"), $data); // Newlinefy all braces and change Windows linebreaks to Linux (much nicer!)
    $data = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $data); // Regex identifies all extra empty lines produced by the str_replace above. It is quicker to do it like this than deal with a more complicated regular expression above.
    $data = str_replace("<?php", "<script>", highlight_string("<?php \n" . $data . "\n?>", true));

    $data = explode("\n", str_replace(array("<br />"), array("\n"), $data));

# experimental tab level highlighting
    $tab = 0;
    $output = '';

    foreach ($data as $line) {
        $lineecho = $line;
        if (substr_count($line, "\t") != $tab) {
            $lineecho = str_replace("\t", "", trim($lineecho));
            $lineecho = str_repeat("\t", $tab) . $lineecho;
        }
        $tab = $tab + substr_count($line, "{") - substr_count($line, "}");
        if ($flush_on_closing_brace && trim($line) == "}") {
            $output .= '}';
        } else {
            $output .= str_replace(array("{}", "[]"), array("<span style='color:" . $c_string . "!important;'>{}</span>", "<span style='color:" . $c_string . " !important;'>[]</span>"), $lineecho . "\n"); // Main JS specific thing that is not matched in the PHP parser
        }

    }

    $output = str_replace(array('?php', '?&gt;'), array('script type="text/javascript">', '&lt;/script&gt;'), $output); // Add nice and friendly <script> tags around highlighted text

    return '<pre id="code_highlighted">' . $output . "</pre>";
}
echo format_javascript('console.log("Here is some highlighted JS code using a single function !");') ;