Location: PHPKode > scripts > Nijikodo > ccampbell-nijikodo-a0508c1/demo.php
<?php require 'lib/Nijikodo/Parser.php'; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Nijikodo demo page</title>
    <link href="css/default.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
        .niji_code { width: 650px; }
        h2.samples { margin-top: 50px; }
<p>by <a href="http://www.craigiam.com">craig campbell</a></p>
    Nijikodo is an object oriented code syntax highlighting library written in PHP.<br />
    Current supported languages include CSS, HTML, JavaScript, and PHP.  The generic regex patterns work for other languages as well.

<!-- usage instructions - view this part in a browser not as raw source -->
<p>php 5.3</p>

<p>to install put the lib directory somewhere in your include path:</p>
<?php Nijikodo\Parser::captureStart(); ?>
require 'lib/Nijikodo/Parser.php';

// you can optionally set a css class prefix (defaults to 'niji_')

<p>to syntax highlight code from a form field:</p>
$text = $_POST['text'];
$text = Nijikodo\Parser::process($text);

<p>use these special tags in the form field to specify that this block of text is code:</p>
// this is where the code goes

<p>this can also be done directly in a view using output buffering like so:</p>
<!-- begin capturing code -->
&lt;?php Nijikodo\Parser::captureStart(); ?&gt;
// code goes here
<!-- output what has been captured -->
&lt;?php echo Nijikodo\Parser::output(); ?&gt;

<p>additionally if you have a string that you already know is a specific type of code you can do this:</p>
$string = '&lt;p&gt;this is my string&lt;/p&gt;';
$html_string = Nijikodo\Parser::toHtml($string, 'html');

<p>finally if you would like to run your string through some other functions you can tokenize the code blocks and fill them back in later</p>
$text = $_POST['text'];
$text = Nijikodo\Parser::tokenizeCodeBlocks($text);
$text = nl2br($text);
$text = Nijikodo\Parser::replaceTokens($text);

<!-- samples -->
<h2 class="samples">samples</h2>

<!-- php code sample -->
// this is some sample php code
$i = 0;
for ($i = 0; $i < 25; ++$i) {
    echo $i;

function customFunction()
    return mt_rand(1, 100);

$fruits = array('banana', 'strawberry', 'blueberry', 'apple', 'blackberry');


foreach ($fruits as $key => $value) {
    echo $value;

<!-- javascript code sample -->
// open external links in a new window using jquery
$(document).ready(function() {
    $("a[rel='external']").live("click", function() {
        $(this).attr("target", "_blank");

<!-- html code sample -->
<!-- html with php inside of it! -->
<ul class="user_list">
    &lt;?php foreach ($this->users as $user): ?&gt;
        <li><a href="&lt;?php echo $user->getUrl(); ?&gt;">&lt;?php echo $user->getName(); ?&gt;</a></li>
    &lt;?php endforeach; ?&gt;

<!-- css code sample (limit to 300 pixel height) -->
/* css used for this demo */
.niji_code {
    font-family: 'monaco', courier, monospace;
    font-size: 11.5px;
    overflow: auto;
    margin-top: 10px;
    margin-bottom: 10px;
    padding: 10px;
    background: #03050A;
    color: #fff;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    white-space: pre-wrap;

.niji_html {
    color: #7F90AA;

.niji_keyword, .niji_define {
    color: #FBDE2D;

.niji_int {
    color: #D8FA3C;

.niji_comment {
    color: #E40700;

.niji_class, .niji_function {
    color: #8DA6CE;

.niji_string {
    color: #61CE3C;

.niji_default {
    color: #fff;

.niji_tag {
    color: #7F90AA;

.niji_method {
    color: #FF6400;

<?php echo Nijikodo\Parser::output(); ?>
Return current item: Nijikodo