本机PHP函数突出显示javascript


native php function to highlight javascript?

是否有任何本地PHP函数作为highlight_string();但是对于javascript ?

或者,如果没有,是否有任何PHP函数(自制)来做?

编辑:我想使用PHP函数来COLORIZE javascript

我在格西取得了巨大的成功。易于使用和集成在您的应用程序中,它支持许多语言。

我知道你想要一个用PHP写的语法高亮。这个人(Geshi)过去曾为我工作过:

http://qbnz.com/highlighter/

是,PHP函数highlight_string()是PHP的本地PHP函数

No.

但是有很多javascript库在几种语言上做语法高亮,从bash脚本到PHP和javascript。

eg,如snippet (JQuery)或JQuery。语法(我最喜欢的)

在这里你可以找到一个很棒的库,它可以使用javascript和css类在大量语言中进行语法高亮显示。

没有原生的php函数来做这个,所以你必须使用现有的库,或者你必须自己写一些东西。

最快的方法-你也可以用一个小技巧使用PHP函数"highlight_string"(捕获函数输出并删除前后PHP标签):

$source = '... some javascript ...';
// option 1 - pure JS code
$htmlJs = highlight_string('<?php '.$source.' ?>', true);
$htmlJs = str_replace(array('&lt;?php&nbsp;', '&nbsp;?&gt;'), array('', ''), $htmlJs);
// option 2 - when mixing up with PHP code inside of JS script
$htmlJs = highlight_string('START<?php '.$source.' ?>END', true);
$htmlJs = str_replace(array('START<span style="color: #0000BB">&lt;?php&nbsp;</span>', '&nbsp;?&gt;END'), array('', ''), $htmlJs);
// check PHP INI setting for "highlight.keyword" (#0000BB) - http://www.php.net/manual/en/misc.configuration.php#ini.syntax-highlighting

没有本地函数,但与其使用全栈库来突出显示一些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>";
}

用法:

echo format_javascript('console.log("Here is some highlighted JS code using a single function !");') ;

信用:http://css-tricks.com/highlight-code-with-php/
演示:http://css-tricks.com/examples/HighlightJavaScript/

这里有很好的信息。下面是另一个不错的例子:http://code.google.com/p/google-code-prettify/