Code Snippets

  

PHP Source Code


Welcome to Dream.In.Code
Become a PHP Expert!

Join 137,226 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,031 people online right now. Registration is fast and FREE... Join Now!





Numbered Syntax Highlighter

This is a php script that takes a php file as an input and then outputs its code with the standard php highlighting but with line numbers as well.

Submitted By: grimpirate
Actions:
Rating:
Views: 898

Language: PHP

Last Modified: March 19, 2007
Instructions: Change the variable $filename to denote the file whose syntax you wish to highlight. ONLY WORKS WITH FIREFOX.

Snippet


  1. <html>
  2. <head>
  3. <title>Grim PHP Syntax Highlighter</title>
  4. <style type="text/css">
  5. body {
  6.      font-family: Courier New;
  7.      font-size: 10pt;
  8.      white-space: nowrap;
  9. }
  10. div.lineNumbers {
  11.      float: left;
  12.      text-align: right;
  13.      margin-right: 8px;
  14.      padding-right: 8px;
  15.      padding-left: 8px;
  16.      border-right: 1px #999999 solid;
  17.      color: #777777;
  18. }
  19. div.sourceCode { 
  20.      float: middle;
  21. }
  22. span.v {
  23.      color: #0000BB;
  24. }
  25. span.k {
  26.      color: #007700;
  27. }
  28. span.s {
  29.      color: #DD0000;
  30. }
  31. span.c {
  32.      color: #FF8000;
  33. }
  34. </style>
  35. </head>
  36. <body>
  37. <?php
  38. $filename = 'forum/admin.php';
  39. ob_start();
  40. highlight_file($filename);
  41. $source = ob_get_contents();
  42. ob_end_clean();
  43. $source = explode(chr(13), $source);
  44. $source = implode('', $source);
  45. $source = explode(chr(10), $source);
  46. $source = implode('', $source);
  47. $source = explode('<br />', $source);
  48. $source[0] = substr($source[0], 35);
  49. $source[count($source) - 1] = substr($source[count($source) - 1], 0, strlen($source[count($source) - 1]) - 14);
  50. for($i = 0; $i < count($source) - 1; $i++){
  51.      $source[$i] .= '<br />';
  52. }
  53. echo '<div class="lineNumbers">' . "\n";
  54. for($i = 0; $i < count($source); $i++){
  55.      echo $i + 1 . '<br />' . "\n";
  56. }
  57. echo '</div>' . "\n";
  58. echo '<div class="sourceCode">' . "\n";
  59. $source = implode("\n", $source);
  60. $source = str_replace('<span style="color: #0000BB">', '<span class="v">', $source);
  61. $source = str_replace('<span style="color: #007700">', '<span class="k">', $source);
  62. $source = str_replace('<span style="color: #DD0000">', '<span class="s">', $source);
  63. $source = str_replace('<span style="color: #FF8000">', '<span class="c">', $source);
  64. echo $source;
  65. echo '</div>' . "\n";
  66. ?>
  67. </body>
  68. </html>

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month