JetCracker

Life-time learner's blog

PHP: Syntax highlighting with GeSHi

One day I needed to add some source code examples to the web-site. But there was no features for highlighting syntax. I decided to create my own tool for highlighting source code. Here I will show you how to do it using PHP with GeSHi.

To begin with, make sure that you installed Apache server with PHP 5. To highlight syntax we will use GeSHi. To install it in Ubuntu I used a command

$ sudo apt-get install php-geshi

Our tool will be able to highlight different programming languages (such as PHP, Java, Python, C++, etc.) and convert it into HTML code.

In some folder (e.g. syntax) in your Apache htdocs directory create the following files:

index.php

<html>
<head><title>Syntax Highlighter!</title></head>
<style>
p, div, body {font-size: 13px; font-family: Arial; }
.listing {background-color: #FFFCCC; border-width: 1px 1px 1px 1px; border-style: solid; padding: 7px;}
</style>
<body>
<?php if($_POST['code']!=""): ?>
<?php include "highlight.php"; ?>
<?php endif; ?>

<p><form method="post" action="#">
Choose code language:
<select name="lang">
  <option value="cpp" <?php if($_POST['lang']=="cpp") echo "selected=1" ?>>C++</option>
  <option value="css" <?php if($_POST['lang']=="css") echo "selected=1" ?>>CSS</option>
  <option value="php" <?php if($_POST['lang']=="php") echo "selected=1" ?>>PHP</option>
  <option value="java" <?php if($_POST['lang']=="java") echo "selected=1" ?>>Java</option>
  <option value="scala" <?php if($_POST['lang']=="scala") echo "selected=1" ?>>Scala</option>
  <option value="Bash" <?php if($_POST['lang']=="Bash") echo "selected=1" ?>>Bash</option>
  <option value="python" <?php if($_POST['lang']=="python") echo "selected=1" ?>>Python</option>
  <option value="html4strict" <?php if($_POST['lang']=="html4strict") echo "selected=1" ?>>HTML 4 Strict</option>
</select>
<input type="checkbox" name="numbers" <?php if($_POST['numbers']!="") echo "checked=1" ?>>Line numbers</input>
<p><textarea cols="100" rows="20" name="code">
<?php if($_POST['code']!="") echo $_POST['code']; ?>
</textarea>
<p><input type="submit">
</form>
<hr>
<p align="center">&copy;&nbsp;Created by <strong>Danon</strong>. Powered by <strong>GeSHi</strong></p>
</body>
</html>

highlight.php

<p><div class="listing">
<?php
  // Подключаем библиотеку
  include_once '/usr/share/php-geshi/geshi.php';
  //пример кода для подсветки
  $source = $_POST['code'];
  //язык кода
  $language = $_POST['lang'];
  // Создаем GeSHi-объект
  $geshi = new GeSHi($source, $language);
  if($_POST['numbers']!="")
    $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS, 0);
  // и печатаем результат
  $res = $geshi->parse_code();
  echo $res;
?>
</div>
</p>
<p>Html code:
<br><textarea cols="100" rows="7">
<div class="listing"><?php echo $res; ?></div>
</textarea>

GeSHi has many different parameters and you can adjust it to your specific needs. Here is what I got:

The working syntax highlighter can bee seen at kenny.kreios.ru.

Advertisements

2 responses to “PHP: Syntax highlighting with GeSHi

  1. Kevin Xi March 13, 2012 at 05:50

    Reblogged this on Kevin's Weblog and commented:
    Nice article in highlighting code with PHP.

  2. Pingback: Spring 2012: Overall stats « JetCracker

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: