How do you display your php source code with highlight or view source?

梦想的初衷 提交于 2019-12-13 01:50:47

问题


How do you display your php source code with highlight or view source or is there more alternatives to this?


回答1:


PHP has two native functions that might be of interest: highlight_file() and highlight_string(). If neither of those are ideal, you could also use Google Code Prettify to achieve this result. This is the solution many use, including StackOverflow itself.

Alternatives:

  • SyntaxHighlighter
  • SHJS
  • jQuery Chili
  • Lighter for MooTools
  • GeSHi



回答2:


You can use the php highlight_file function to echo the source of a file with syntax highlighting.




回答3:


On many servers if you give it a .phps file extension the source code will be displayed and highlighted.




回答4:


GeSHi - Generic Syntax Highlighter is another opensource javascript library.




回答5:


I do my editing in gvim which can be configured to do syntax sensitive code highlighting (amongst other things).

PHP has a builtin function which converts a string to coloured HTML

http://php.net/manual/en/function.highlight-string.php

so...

<?php

print highlight_string(file_get_contents(__FILE__));

?>

demonstrates this.

You might also wnat to have a lokk at GeSHi

http://qbnz.com/highlighter/

HTH

C.




回答6:


I don't like to load my scripts form foreign hosts each time - especially not from search engines like PRISM. This is not that secure and load-time-killing. If you have experience with RegExp it's quick done.

Here is a snippet I used for a short PHP tutorial. You need to complete it with some CSS, commands in the last RegExp and an onload='init()' in the body-tag.

function init() { 
   var codes= document.getElementsByTagName('code')
      ,str= ''
      ,len= codes.length
      ,c= 0
      ;
   for(c; c < len; c++){ 
      str = codes[c].textContent;   
      str=str.replace(/(\bnull\b|\b[0-9]+\b|\btrue\b|\bfalse\b|"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*')/g,"<span class=\"text\">$1</span>");
      str=str.replace(/(\(|\)|\[|\]|\{|\})/g,"<span class='bracket'>$&</span>");
      str=str.replace(/\$[a-z]\w*/gi,"<span class='variable'>$&</span>");
      str=str.replace(/( \! | \!\= | \!== | = | == | === | > | >= | < | <= | and | or )/g,"<span class='operator'>$&</span>");
      str=str.replace(/\b(for|function|return|unset|count|global|if|else|else if|intval|int|array)\b/g,"<span class='command'>$1</span>");
      codes[c].innerHTML=str;
   }
}


来源:https://stackoverflow.com/questions/2155588/how-do-you-display-your-php-source-code-with-highlight-or-view-source

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!