2011-07-13 8 views
2

ネイティブPHPの機能はhighlight_string();ですが、javascriptはありますか?javascriptを強調するネイティブPHP関数?

もしそうでなければ、PHPの機能(手作り)はありますか?

編集:私はjavascriptの

+0

あなたは、私が推測JavaScriptを強調するためにネイティブ関数を意味ですか?そうでなければあなた自身の質問に答えました;) – hoppa

+0

はい、そうです。私はPHPで色分けする方法を知っているが、私は知ってほしい、どのようにPHPでjavascriptを着色する:) – genesis

答えて

2

私はGeSHiで大成功を収めました。使いやすく、アプリに統合し、ロットのをサポートしています。

+0

が良いように見える、私はそれを試してみましょう。しかし、今私は単機能を探しています – genesis

+1

これを行うための機能はありません。ハイライト表示は複雑すぎるため、単一の機能でキャプチャして理解することはできません。あなたが単一の機能を見つけたら、それは1つの扱いにくいモンスターになります! PHPの 'highlight_string()'のみ*はPHP内で隠されているため、単一の関数のように見えます。 –

1

はい、PHP機能highlight_stringを(色付けするPHP関数を使用したい)PHPのネイティブPHP関数です。

+1

私はOPがjavascriptのための手段を信じています..タイトルを確認してください –

+0

オオウ、よく今は別の質問です:) :) –

+0

私はJavaScriptをハイライトすることができるようにPHP関数を見つける:) – genesis

1

しかし、PHPとJavaScriptにはbash-スクリプトから、いくつかの言語に構文強調表示を行う のJavaScriptライブラリがたくさんあります。 hereオーバー snippet(jQueryの)または jQuery.Syntax(私のお気に入り)

1

ようにあなたはJavaScriptを使用して言語の大量やCSSクラスの構文の強調表示を可能に優れたライブラリを見つけることができます

例えば、。

これを行うためのネイティブPHP関数はありませんので、既存のライブラリを使用するか、自分で何かを書く必要があります。

+0

申し訳ありませんが、私の編集の質問で見てください。私はPHP関数 – genesis

2

PHPで書かれたSyntax Highligherが必要です。この1(Geshi)は、過去に私のために働いている:

http://qbnz.com/highlighter/

+0

私はデモで見たように素晴らしいツールですが、今は単機能を探しています:)私はあなたに+1を与えるでしょう – genesis

+0

'function highlight($ string、$ language){include_once 'geshi.php'; $ geshi =新しいGeSHi($文字列、$言語); return $ geshi-> parse_code();} ':D – madflow

1

最速の方法 - あなたは少しトリック でも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 
2

ませネイティブ機能ではなく、ちょうどあなたがこの単一の機能を使用することができます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/

関連する問題