BBLOG PHP Syntax Highlighting Tutorial

March 30, 2005

I'll quickly describe how to get PHP Syntax highlighting using BBCode in your BBLOG application using GESHI (you can apply this same technique to any application). What we're going to do is enable our blog to support [ php] tags

Step 1.
Download Geshi from http://sourceforge.net/project/showfiles.php?group_id=114997

Step 2.
Unzip the files to your webdirectory that your blog is in. For example mine is located in litfuel.net/plush/bblog/bBlog_plugins/geshi/

Step 3.
open up the file modifier.bbcode.php script in bblog/bBlog_plugins/ folder. At the top of this file add the following
  1.  
  2. // this is the path to the geshi.php file yours will obviously be different so change this!
  3. $include_path = "/plush/bblog/bBlog_plugins/geshi/";
  4. // include main geshi file
  5. include_once($include_path."geshi.php");


Step 4.
Add this block of code RIGHT BEFORE THE RETURN statement in the smarty_modifier_bbcode function. So locate
  1. return (nl2br($ret));
and add the code below right above it.

  1.  
  2. /*------------------------------------------------------------------------------------------*/
  3. // Jim Plush Add on for PHP Syntax Highlighting questions? <a href="mailto:jiminoc@gmail.com">jiminoc@gmail.com</a>
  4. // CHECK FOR PHP TAGS
  5. $regex = '/\[php\](.*?)\[\/php\]/si';
  6. // GRAB THE PHP CODE WE WANT TO HIGHLIGHT IN $matches[1]
  7. preg_match_all($regex, $ret, $matches);
  8. // set path to the geshi FILES folder - notice I have the double geshi now this is where the php.php file is located
  9. $path = "/litfuel.net/plush/bblog/bBlog_plugins/geshi/geshi/";
  10. // now we have to loop through all our matches because we can have multiple php brackets in our post
  11. $cnt = count($matches[1]);
  12. for($i=0; $i < $cnt; $i++)
  13. {
  14. // Create a GeSHi object where php is the language we want to use
  15. $geshi = new GeSHi($matches[1][$i], 'php', $path);
  16. // lets enable line numbers so people can comment based on the line
  17. $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
  18. $phpcode = $geshi->parse_code();
  19. $ret = str_replace($matches[0][$i], $phpcode , $ret);
  20. }
  21. /*------------------------------------------------------------------------------------------*/


thats it! now when you write in your blog whenever you want to use PHP Syntax Highlighting just put [ php ] my php stuff [/ php ] tags in and use the BBCode Entry Modifier :)
There are tons of advanced features you could add with geshi but I tried to keep it super simple for the moment.

Comments

RSS feed for comments on this post.

  1. Anonymous says:
    May 12, 2005 @ 19:42 — Reply

    Cool. Have to check it out.

  2. johno says:
    June 2, 2005 @ 14:47 — Reply

    Have you tried fshl? FSHL? Supports multi-syntax highlighting, generates output really fast and has really nice stylesheet ;)

  3. slavka says:
    July 22, 2005 @ 00:31 — Reply

    I think the following works fine too.... all other steps keep the same apart from the php code parsing try the one below :) //************************************************************************** // php syntax... //************************************************************************** $regex = '/\[php\](.*?)\[\/php\]/si'; preg_match_all($regex, $ret, $matches); $path = "c:/apachefriends/xampp/htdocs/bblog/bblog/bBlog_plugins/geshi/geshi"; $cnt = count($matches[1]); for($i=0; $i $content .= $matches[1][$i]; if ($i == ($cnt-1)) { $replacethis = $matches[0][$i]; } else { $ret = str_replace($matches[0][$i], $i , $ret); } } $geshi = new GeSHi($content, 'PHP', $path); $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS); $geshi->set_url_for_keyword_group('php', 'http://www.php.net/{FNAME}'); $phpcode = ''.$geshi->parse_code().''; $ret = str_replace($replacethis, $phpcode , $ret); //echo $phpcode; //**************************************************************************

  4. Anonymous says:
    July 22, 2005 @ 00:32 — Reply

    [php] //************************************************************************** // php syntax... //************************************************************************** $regex = '/\[php\](.*?)\[\/php\]/si'; preg_match_all($regex, $ret, $matches); $path = "c:/apachefriends/xampp/htdocs/bblog/bblog/bBlog_plugins/geshi/geshi"; $cnt = count($matches[1]); for($i=0; $i $content .= $matches[1][$i]; if ($i == ($cnt-1)) { $replacethis = $matches[0][$i]; } else { $ret = str_replace($matches[0][$i], $i , $ret); } } $geshi = new GeSHi($content, 'PHP', $path); $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS); $geshi->set_url_for_keyword_group('php', 'http://www.php.net/{FNAME}'); $phpcode = ''.$geshi->parse_code().''; $ret = str_replace($replacethis, $phpcode , $ret); //echo $phpcode; //************************************************************************** [/php] :)

  5. Anonymous says:
    July 22, 2005 @ 00:33 — Reply

     <?php echo "hello world";function &_loadElement($event, $type, $args) { $type = strtolower($type); if (!HTML_QuickForm::isTypeRegistered($type)) { return PEAR::raiseError(null, QUICKFORM_UNREGISTERED_ELEMENT, null, E_USER_WARNING, "Element '$type' does not exist in HTML_QuickForm::_loadElement()", 'HTML_QuickForm_Error', true); } $className = $GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES'][$type][1]; $includeFile = $GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES'][$type][0]; include_once($includeFile); $elementObject =& new $className(); for ($i = 0; $i < 5; $i++) { if (!isset($args[$i])) { $args[$i] = null; } } $err = $elementObject->onQuickFormEvent($event, $args, $this); if ($err !== true) { return $err; } return $elementObject; } // end func _loadElement?>

  6. Anonymous says:
    July 22, 2005 @ 00:33 — Reply

    sorry for the multiple posts... but wish there was a php code allowance here :)

  7. Xenom says:
    April 27, 2006 @ 14:23 — Reply

    Has been a while ago you posted this. I was looking for something like this for weeks, about an hour ago i found GeSHI, but unfortunatly no 'BB' code support like [ php] [ /php]. This fixes it all :) Many thanks!

  8. Bert says:
    August 4, 2007 @ 10:44 — Reply

    There is no modifier.bbcode.php file included in the zip. I had to read the geshi-doc.txt file ("2.3: Basic Usage" section) to get it to work. Great script!

  9. Louis Vuitton Handbags Replica says:
    January 31, 2010 @ 20:34 — Reply

    Comment pending moderation

  10. ipod to mac says:
    March 18, 2010 @ 06:36 — Reply

    Comment pending moderation

  11. ipod to mac says:
    March 18, 2010 @ 06:38 — Reply

    Comment pending moderation

  12. room dividers nyc says:
    March 18, 2010 @ 06:41 — Reply

    Comment pending moderation

  13. Anti Premature Ejaculation says:
    March 29, 2010 @ 05:17 — Reply

    Comment pending moderation

  14. blu ray ripper says:
    April 18, 2010 @ 04:35 — Reply

    Comment pending moderation

  15. AGPtek battery says:
    May 6, 2010 @ 21:35 — Reply

    Comment pending moderation

  16. werjwerkw says:
    May 10, 2010 @ 15:32 — Reply

    Comment pending moderation

  17. HP0-J33 says:
    May 19, 2010 @ 00:25 — Reply

    Comment pending moderation

  18. online forex trading says:
    May 21, 2010 @ 20:58 — Reply

    Comment pending moderation

  19. virbram five fingers says:
    June 4, 2010 @ 21:02 — Reply

    Comment pending moderation

  20. traiteur hallal says:
    June 11, 2010 @ 17:08 — Reply

    Comment pending moderation

  21. E20-322 says:
    June 12, 2010 @ 17:57 — Reply

    Comment pending moderation

  22. watches replica says:
    June 19, 2010 @ 00:44 — Reply

    Comment pending moderation

  23. Brand Lee says:
    June 23, 2010 @ 02:00 — Reply

    Comment pending moderation

  24. chanel says:
    June 23, 2010 @ 02:02 — Reply

    Comment pending moderation

  25. louis vuitton replica says:
    June 24, 2010 @ 01:53 — Reply

    Comment pending moderation

  26. last longer in bed says:
    June 24, 2010 @ 14:35 — Reply

    Comment pending moderation

Leave a Comment

Line and paragraph breaks automatic, HTML allowed: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <code> <em> <i> <strike> <strong>

Comments disabled due to spammers being losers that lead sad lives.