本地php函数来突出显示javascript?

发布于 2024-11-24 04:22:51 字数 224 浏览 1 评论 0原文

是否有任何本地 PHP 函数 highlight_string(); 但适用于 javascript ?

或者,如果没有,是否有任何 PHP 函数(自制)可以做到这一点?

编辑:我想使用 PHP 函数对 javascript 进行着色

Is there any native PHP function as highlight_string(); but for javascript ?

Or, if not, is there any PHP function (homemade) to do it?

EDIT: I want to use PHP function to COLORIZE javascript

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

浊酒尽余欢 2024-12-01 04:22:52

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

I have had great success with GeSHi. Easy to use and integrate in your app and it supports a lot of languages.

泪眸﹌ 2024-12-01 04:22:52

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

http://qbnz.com/highlighter/

I understand you want a Syntax Highligher written in PHP. This one (Geshi) has worked for me in the past:

http://qbnz.com/highlighter/

爱给你人给你 2024-12-01 04:22:52

是的,PHP 函数highlight_string() 是PHP 的原生PHP 函数。

Yes, the PHP function highlight_string() is a native PHP function for PHP.

情深缘浅 2024-12-01 04:22:52

不。

但是有很多 javascript 库可以对多种语言进行语法突出显示,
从 bash 脚本到 php 和 javascript。

例如,像 snippet (JQuery) 或 jQuery.Syntax (我最喜欢的)

No.

But there are a lot of javascript libraries that do syntax-highlight on several languages,
from bash-scripting to php and javascript.

eg, like snippet (JQuery) or jQuery.Syntax (my favorite)

岁月如刀 2024-12-01 04:22:52

此处,您可以找到一个优秀的库,它可以使用 javascript 和 css 类在大量语言中实现语法突出显示。

没有本地 php 函数可以执行此操作,因此您要么必须使用现有的库,要么必须自己编写一些东西。

Over here you can find an excellent library which enables syntax highlighting in a large amount of languages using javascripts and a css class.

There is no native php function to do this, so either you have to use existing libraries or you have to write something yourself.

罪#恶を代价 2024-12-01 04:22:52

最快的方法 - 你也可以使用 PHP 函数“highlight_string”,并有一个小技巧
(捕获函数输出并删除前导/尾随 PHP 标签):

$source = '... some javascript ...';

// option 1 - pure JS code
$htmlJs = highlight_string('<?php '.$source.' ?>', true);
$htmlJs = str_replace(array('<?php ', ' ?>'), 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"><?php </span>', ' ?>END'), array('', ''), $htmlJs);
// check PHP INI setting for "highlight.keyword" (#0000BB) - http://www.php.net/manual/en/misc.configuration.php#ini.syntax-highlighting

Fastest way - you can use also PHP function "highlight_string" with a little trick
(capture function output and remove leading/trailing PHP tags):

$source = '... some javascript ...';

// option 1 - pure JS code
$htmlJs = highlight_string('<?php '.$source.' ?>', true);
$htmlJs = str_replace(array('<?php ', ' ?>'), 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"><?php </span>', ' ?>END'), array('', ''), $htmlJs);
// check PHP INI setting for "highlight.keyword" (#0000BB) - http://www.php.net/manual/en/misc.configuration.php#ini.syntax-highlighting
阳光下的泡沫是彩色的 2024-12-01 04:22:52

没有本机函数,但您可以使用这个单一函数,而不是使用完整的堆栈库来突出显示某些 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', '?>'), array('script type="text/javascript">', '</script>'), $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/

No native function, but rather than using a full stack library just to highlight some javascript you can use this single function :

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', '?>'), array('script type="text/javascript">', '</script>'), $output); // Add nice and friendly <script> tags around highlighted text

    return '<pre id="code_highlighted">' . $output . "</pre>";
}

Usage :

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

Credit :
http://css-tricks.com/highlight-code-with-php/
Demo :
http://css-tricks.com/examples/HighlightJavaScript/

国粹 2024-12-01 04:22:52

这里有很好的信息。这是另一个不错的:http://code.google.com/p/google-代码美化/

Well nice info here . Here is another nice one : http://code.google.com/p/google-code-prettify/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文