使用PHP显示带有语法高亮的文件


Display files with syntax highlighting using PHP

我正在使用php(不使用Java),其中我需要显示源代码文件(Java,c,c++,Python等)在网页上语法高亮显示基于web的应用程序。我不知道如何显示源代码文件。

选项之一是使用现有的语法高亮显示,如google高亮显示。

设置非常简单。你所要做的就是在你的html页面中包含<pre>部分的代码,并应用一个类属性,即编程语言。

<pre name="code" class="c-sharp">
class Foo
{
}
</pre>

16 Free Javascript Code Syntax highlighter For Better Programming有一个非常详尽的选项列表。在这里重复,以防网站宕机

  1. SyntaxHighlighter
  2. GeSHi -通用语法荧光笔
  3. quickhighlighter
  4. google-code-prettify
  5. pygments。
  6. HIGHLIGHT.JS
  7. Lighter.js -使用MooTools编写的语法高亮笔
  8. SHJS - JavaScript语法高亮显示
  9. CodePress -在线实时语法高亮编辑器
  10. Chili jQuery代码荧光笔插件
  11. 高亮-代码&语法高亮显示——Andre Simon
  12. BeautyOfCode: jQuery Plugin for Syntax highlight
  13. js - JavaScript语法高亮笔
  14. Ultraviolet - Syntax highlight Engine
  15. DlHighlight - JavaScript语法高亮引擎
  16. JavaScript语法高亮显示

语法高亮用于显示源代码程序的色彩,以便读者在集成后可以轻松阅读/理解您的代码。在这个程序中,我添加了各种元素(保留词,括号,注释,引号等)来突出显示。您可以根据您的Web应用程序/编程博客添加/修改此代码。

此语法高亮代码独立于任何语言,因此您可以添加与任何编程语言的集成。

请从我的技术博客- http://www.algonuts.info/how-to-develop-a-source-code-syntax-highlighter-using-php.html找到源代码

<?php
include_once("keywords.php");
class highlighter {
    private $fileName;
    private $fileNameColor;
    private $fileExtension;
    private $parenthesisColor;
    private $insideParenthesisColor;
    private $keywordColor;
    private $backGroundColor;
    private $borderColor;
    private $leftBorderColor;
    private $quotesColor;
    private $commentColor;
    public function __construct() {
        $this->fileName = "";
        $this->fileExtension = "";
        //Color Configuration
        $this->fileNameColor = "#286090";
        $this->keywordColor = "green";
        $this->backGroundColor = "#fdfefe";
        $this->borderColor = "#e3e3e3";
        $this->leftBorderColor = "#605a56";
        $this->parenthesisColor = "#ec7700";
        $this->insideParenthesisColor = "#ec7700";
        $this->bracketColor = "#ec7700";
        $this->insideBracketColor = "#ec7700";
        $this->quotesColor = "#6a2c70";
        $this->commentColor = "#b8b0b0";
    }
    public function applycolor($fileLocation = "") {
        if($fileLocation == "")
        {   return; }
        else
        {
            if(file_exists($fileLocation)) {
                $temp = explode("/",$fileLocation);
                $this->fileName = trim(end($temp));
                $temp = explode(".",$this->fileName);
                $this->fileExtension = trim(end($temp));
                $fileContent = trim(file_get_contents($fileLocation, true));
                $fileContent = htmlentities($fileContent,ENT_NOQUOTES);
                if($fileContent == "")
                {   return; }
            }
            else
            {   return; }
        }
        $line = 1;
        $outputContent = "<div class='"divblock'"><b>".$line."</b>  ";
        $characterBuffer = "";
        $blockFound = 0;
        $blockFoundColor = array();
        $parenthesisFound = 0;
        $bracketFound = 0;
        $counter = 0;
        $lastCharacter = "";
        $contentSize = strlen($fileContent);                    
        while($counter < $contentSize) {
            $character = $fileContent[$counter];
            $code = intval(ord($character));
            if($blockFound == 0 && (($code >= 97 && $code <= 122) || ($code >= 65 && $code <= 90))) //Fnd alphabetic characters
            {   $characterBuffer .= $character; }
            else
            {
                if($code == 10) { //Find EOL (End of Line)
                    if($this->checker($characterBuffer))                
                    {   $characterBuffer = "<font color='".$this->keywordColor."'>".$characterBuffer."</font>"; }
                    $line++;
                    if($blockFound == 0)
                    { $outputContent .= $characterBuffer."</div>".$character."<div class='"divblock'"><b>".$line."</b>  "; }
                    else
                    { $outputContent .= $characterBuffer."</font></div>".$character."<div class='"divblock'"><b>".$line."</b>  <font color='".$blockFoundColor[$blockFound-1]."'>"; }
                    $characterBuffer = "";
                }
                else if($code == 32) {  //Find Space
                    if($characterBuffer != "") {
                        if($this->checker($characterBuffer))                
                        {   $outputContent .= "<font color='".$this->keywordColor."'>".$characterBuffer."</font>".$character;   }
                        else
                        {   $outputContent .= $characterBuffer.$character;  }
                        $characterBuffer = "";
                    }
                    else
                    {   $outputContent .= $character;   }
                }
                else if($character == "'"" || $character == "'") {  //Find Quotes
                    if($characterBuffer != "")
                    {
                        if($this->checker($characterBuffer))                
                        {   $outputContent .= "<font color='".$this->keywordColor."'>".$characterBuffer."</font>";  }
                        else
                        {   $outputContent .= $characterBuffer; }
                        $characterBuffer = "";
                    }
                    $outputContent .= "<font color='".$this->quotesColor."'>".$character;
                    $foundCharacter = $character;
                    $counter++;
                    while($counter < $contentSize) {
                        $character = $fileContent[$counter];
                        if($character == $foundCharacter) {
                            $outputContent .= $character;
                            if($lastCharacter == "''") {
                                $lastCharacter = "";
                            }
                            else
                            {   break;  }
                        }
                        else if($character == "''" && $lastCharacter == "''") {
                            $outputContent .= $character;
                            $lastCharacter = "";
                        }
                        else
                        {
                            $lastCharacter = $character;
                            $code = intval(ord($character));
                            if($code != 10) 
                            {   $outputContent .= $character;   }
                            else
                            {   
                                $line++;
                                $outputContent .= "</font></div>".$character."<div class='"divblock'"><b>".$line."</b>  <font color='".$this->quotesColor."'>";     
                            }
                        }
                        $counter++;
                    }
                    $outputContent .= "</font>";
                }
                else if($character == "(" || $character == ")") {   //Find Parenthesis
                    if($characterBuffer != "")
                    {
                        if($this->checker($characterBuffer))                
                        {   $outputContent .= "<font color='".$this->keywordColor."'>".$characterBuffer."</font>";  }
                        else
                        {   $outputContent .= $characterBuffer; }
                        $characterBuffer = "";
                    }
                    if($parenthesisFound == 0) {
                        $blockFoundColor[$blockFound] = $this->insideParenthesisColor;
                        $outputContent .= "<font color='".$this->parenthesisColor."'>".$character."</font><font color='".$this->insideParenthesisColor."'>";
                        $parenthesisFound++;
                        $blockFound++;
                    }
                    else
                    {
                        if($character == "(") {
                            $parenthesisFound++;
                        }
                        if($character == ")") {
                            $parenthesisFound--;
                        }
                        if($parenthesisFound == 0) {
                            $outputContent .= "</font><font color='".$this->parenthesisColor."'>".$character."</font>";
                            $blockFound--;
                            unset($blockFoundColor[$blockFound]);
                        }
                        else
                        {   $outputContent .= $character;   }
                    }
                }
                else if($character == "[" || $character == "]") {   //Find Bracket
                    if($characterBuffer != "")
                    {
                        if($this->checker($characterBuffer))                
                        {   $outputContent .= "<font color='".$this->keywordColor."'>".$characterBuffer."</font>";  }
                        else
                        {   $outputContent .= $characterBuffer; }
                        $characterBuffer = "";
                    }
                    if($bracketFound == 0) {
                        $blockFoundColor[$blockFound] = $this->insideBracketColor;
                        $outputContent .= "<font color='".$this->bracketColor."'>".$character."</font><font color='".$this->insideBracketColor."'>";
                        $bracketFound++;
                        $blockFound++;
                    }
                    else
                    {
                        if($character == "[") {
                            $bracketFound++;
                        }
                        if($character == "]") {
                            $bracketFound--;
                        }
                        if($bracketFound == 0) {
                            $outputContent .= "</font><font color='".$this->bracketColor."'>".$character."</font>";
                            $blockFound--;
                            unset($blockFoundColor[$blockFound]);
                        }
                        else
                        {   $outputContent .= $character;   }
                    }
                }
                else if($character == "/" && (isset($fileContent[$counter+1]) && ($fileContent[$counter+1] == "*" || $fileContent[$counter+1] == "/"))) {   //Find Comment
                    if($characterBuffer != "")
                    {
                        if($this->checker($characterBuffer))                
                        {   $outputContent .= "<font color='".$this->keywordColor."'>".$characterBuffer."</font>";  }
                        else
                        {   $outputContent .= $characterBuffer; }
                        $characterBuffer = "";
                    }
                    $blockFound++;
                    $outputContent .= "<font color='".$this->commentColor."'>".$fileContent[$counter].$fileContent[$counter+1];
                    if($fileContent[$counter+1] == "*") {
                        $counter += 2;
                        $checkCharacter = "*";
                        while($counter < $contentSize) {
                            $outputContent .= $fileContent[$counter];
                            if($fileContent[$counter] == $checkCharacter) {
                                if($checkCharacter == "*") 
                                {   $checkCharacter = "/";  }
                                else 
                                {
                                    $blockFound--;
                                    $outputContent .= "</font>";
                                    break;
                                }
                            }
                            $counter++;
                        }
                    }
                    else
                    {
                        $counter += 2;
                        while($counter < $contentSize) {
                            $character = $fileContent[$counter];
                            $code = intval(ord($character));
                            if($code == 10) {
                                $counter--;
                                $blockFound--;
                                $outputContent .= "</font>";
                                break;
                            }
                            $outputContent .= $character;
                            $counter++;
                        }
                    }
                }
                else if($characterBuffer != "")
                {
                    if($this->checker($characterBuffer))                
                    {   $outputContent .= "<font color='".$this->keywordColor."'>".$characterBuffer."</font>".$character;   }
                    else
                    {   $outputContent .= $characterBuffer.$character;  }
                    $characterBuffer = "";
                }
                else
                {   $outputContent .= $character;   }
            }
            $counter++;
        }
        $outputContent .= "</div>";
        $rerurnData = "<div class='filenamestyle' style='color:".$this->fileNameColor.";'>".$this->fileName."</div>";   //Show filename
        $rerurnData .= "<div><pre><div class='codebox' style='background-color:".$this->backGroundColor.";border: 1px solid ".$this->borderColor.";border-left: 4px solid ".$this->leftBorderColor.";'>".$outputContent."</div></pre></div>";
        return $rerurnData;
    }
    private function checker($value) {
        global $languageKeywords;       
        if(isset($languageKeywords[$this->fileExtension])) {                
            $value = trim($value);
            if(in_array($value,$languageKeywords[$this->fileExtension]))
            {   return 1;   }
            else
            {   return 0;   }
        }       
    }
}
?>