Publish PHP Code on the web – Highlight.js

It is often required to publish some code snippets in the blog on PHP or other popular languages like Python, Ruby… Here is simple way to do this..

Use nice JavaScript code formatting library. Just enter program text in <pre><code></code></pre> tags in any part of post, include CSS and library and voila:

require_once 'Zend.php';
require_once 'Zend/Uri/Exception.php';
require_once 'Zend/Uri/Http.php';
require_once 'Zend/Uri/Mailto.php';

abstract class Zend_Uri
{

  /**
   * Return a string representation of this URI.
   *
   * @see     getUri()
   * @return  string
   */
  public function __toString()
  {
      return $this->getUri();
  }

  static public function factory($uri = 'http')
  {
      $uri = explode(':', $uri, 2);
      $scheme = strtolower($uri[0]);
      $schemeSpecific = isset($uri[1]) ? $uri[1] : '';

      // Security check: $scheme is used to load a class file,
      // so only alphanumerics are allowed.
      if (!ctype_alnum($scheme)) {
          throw new Zend_Uri_Exception('Illegal scheme');
      }
  }
}

This code should be appended into HTML:

<script src="http://yandex.st/highlightjs/5.12/highlight.min.js"></script>
<link rel="stylesheet" href="http://yandex.st/highlightjs/5.12/styles/default.min.css">
<link rel="stylesheet" href="http://yandex.st/highlightjs/5.12/styles/vs.min.css">
<script>
  hljs.initHighlightingOnLoad();
</script>

Library itself is available through Yandex content distribution network(CDN), so no installation is needed.

Links

Thanks to Ivan Sagalaev.

Did you find this post useful? Support the the author ($10)
My Google Profile+

1 comment

  1. remokaal1 week, 2 days ago I’m on my second order with them, first one took a month to arivre. They said something about, that they needed to ship the goods from Italy because of somekind of troubles with the customs, not really sure, I’m in the EU like they are. Complained with the bank, then things started moving. Right now it’s few days less than a month on the second batch and they haven’t shipped yet, answered an email though, at least. And I’m really wondering why I ordered a second time probably because of an EU company and I could pay the correct taxes.+2Was this answer helpful?

Leave a Reply

Your email address will not be published.