2

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

2
  • You mean a native function to highlight javascript I presume? Otherwise you answered your own question ;) Commented Jul 13, 2011 at 20:32
  • yes, you're right. I know how to colorize php by php but I want to know, how to colorize javascript with php :) Commented Jul 13, 2011 at 20:39

8 Answers 8

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

that looks good, I will try it. However, I looking for single-function right now
There's no such thing as a single function to do that. Highlighting is too complex to capture in a single function and be understandable. If you do find a single function then it will be one unwieldy monster! PHP's highlight_string() only looks like a single function because the rest is hidden inside PHP itself.
2

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

http://qbnz.com/highlighter/

2 Comments

it's great tool as I seen on demo, but I'm searching for single-function right now :) I'll give you +1 tho
function highlight($string, $language) { include_once 'geshi.php'; $geshi = new GeSHi($string, $language); return $geshi->parse_code();} :D
1

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

3 Comments

I believe the OP means for javascript..check the title
Oho, well now that's a different question... :)
I want to find PHP function to be able to highlight javascript :)
1

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)

Comments

1

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.

Comments

1

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('&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

Comments

1

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', '?&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>";
}

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/

Comments

0

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

1 Comment

I'm looking for PHP function!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.