Jump to content
xisto Community
Sign in to follow this  
Houdini

Use PHP Highlighted Code With IPB 2.x possibly later versions

Recommended Posts

I have prior to this post developed code to display PHP code within an Invision Board post with highlighting for 1.0 1.2 and 1.3 but just recently developed it for 2.0 and believe it will also work with newer versions here is the code to do this in its entiriity or the MOD if you like and since this forum presently does not have the capabilities I am posting this for m^e to play with and those that would like to try it with their own. Please keep in mind that this has only been tested using version 2.0 (the last free version) I refuse to pay for software if I can make it myself or upgrade it to something even better myself. So this is a MOD and all you need to do is copy it and save it and follow the install as per below.

//////////////////////////////PHP BB Code or IPB Code for Invision board/////////////////////////////////////////////////by Steve DeMarcus upon request here is the MOD for your 2+ IPB site////////////////////////////

Invision Power Board has some great features but one that is missing is the ability to provide PHP code within a post that is highlighted. Well I thought there already was such code, but found that I was wrong and since I am a regular contributor to some forums that do use such code I saw that this was lacking in IPB so I wrote my own MOD. This one allows your user to enter PHP code by using the BB Code of

 and 

or

and 

to show code within a post with highlighting of the the code within those tags. I also have developed the same with buttons either graphic or text based for earlier versions of IPB (prior to 2.0)

////////////////////////////////////////////////////////////////////////////////////////////////////////////

//Notice that I will point out code to find followed by where to place a change in the code find is always in CAPITAL letters like FIND then followed by the code to find in the script. Also there will be an OPEN statement which is the file you need to edit. In this MOD ther is just one file but you will still see the OPEN statement. The file to open is a relative address to the root of your install, so with that said here is the PHP with highlighting modification for IPB 2+

////////////////////////////////////////////////////////////////////////////////////////////////////////////

OPEN sources/lib/post_parser.php

 

FIND the following in the file:

 

//---------------------------------

// Do [ CODE ] tag//Added Note for this post no spaces after [ or before ] because this post won't work right

//---------------------------------

 

$txt = preg_replace( "#\

[code\](.+?)\[/code\]#ies", "\$this->regex_code_tag('\\1')", $txt );

After it ADD:

//---------------------------------
// Do
 tag
		//---------------------------------

		$txt = preg_replace( "#\[php\](.+?)\[/php\]#ies", "\$this->regex_php_tag('\\1')", $txt );

FIND:
		$txt = preg_replace( "#<!--c2-->(.+?)<!--ec2-->#", '[/CODE]'  , $txt );

After it ADD:

		$txt = preg_replace( "#<!--php1-->(.+?)<!--ephp1-->(.+?)<!--php2-->(.+?)<!--ephp2-->#e", "\$this->unconvert_php('\\2')", $txt );

FIND:
//+-----------------------------------------------------------------------------------------
//+-----------------------------------------------------------------------------------------
// UNCONVERT FUNCTIONS
//+-----------------------------------------------------------------------------------------
//+-----------------------------------------------------------------------------------------

After ADD:
 function unconvert_php($txt)

  {

	$txt = str_replace("<br />", "\n", $txt);

	$txt = str_replace("<code>", "", $txt);

	$txt = str_replace("</code>", "", $txt);

	$txt = preg_replace("#\<font(.+?)>#", "", $txt);

	$txt = str_replace("</font>", "", $txt);

	$txt =strip_tags($txt);

	return '[PHP]' . $txt . '

';

 

}

 

FIND:

 

'sql' => array( 'title' => 'SQL' , 'css_top' => 'sqltop' , 'css_main' => 'sqlmain' ),

'html' => array( 'title' => 'HTML' , 'css_top' => 'htmltop' , 'css_main' => 'htmlmain' )

);

 

REPLACE the above WITH:

'sql' => array( 'title' => 'SQL' , 'css_top' => 'sqltop' , 'css_main' => 'sqlmain' ),

'html' => array( 'title' => 'HTML' , 'css_top' => 'htmltop' , 'css_main' => 'htmlmain' ),

'php' => array( 'title' => 'PHP' , 'css_top' => 'phptop' , 'css_main' => 'phpmain')

);

 

FIND:

/**************************************************/

// regex_code_tag: Builds this code tag HTML

//

/**************************************************/

/**************************************************/

Before The /**********...*/ ADD:

/***************************************************************************/

// regex_php_tag: Builds this php tag HTML

//

/***************************************************************************/

 

function regex_php_tag($php="") {

 

global $ibforums;

 

$default = "\[php\]$php\[/php\]";

 

if ($php == "") return;

 

// Too many embedded code/quote/html/sql tags can crash Opera and Moz

 

if (preg_match( "/\[(quote|code|html|sql|php)\].+?\[(quote|code|html|sql|php)\].+?\[(quote|code|html|sql|php)\].+?\[(quote|code|html|sql|php)\].+?\[(quote|code|html|sql|php)\].+?\[(quote|code|html|sql|php)\].+?\[(quote|code|html|sql|php)\]/i", $php) ) {

 

return $default;

 

}

 

$php = @html_entity_decode($php, ENT_QUOTES);

 

$php = str_replace("$", "$", $php);

 

if(substr_count($php, "?>") <= 0)

 

{

 

$php = "<?php\n" . $php . "\n?>";

 

}

 

$php = highlight_string($php, 1);

 

$php = str_replace("<code>", "", $php);

 

$php = str_replace("</code>", "", $php);

 

while(preg_match("#\n$#", $php))

 

{

 

$php = preg_replace("#\n$#", "", $php);

 

}

 

// Take a stab at removing most of the common

 

// smilie characters.

 

//$php = str_replace( "&" , "&", $php );

 

$php = preg_replace( "#<#" , "<", $php );

 

$php = preg_replace( "#>#" , ">", $php );

 

$php = preg_replace( "#"#" , """, $php );

 

$php = preg_replace( "#:#" , ":", $php );

 

$php = preg_replace( "#\[#" , "[", $php );

 

$php = preg_replace( "#\]#" , "]", $php );

 

$php = preg_replace( "#\)#" , ")", $php );

 

$php = preg_replace( "#\(#" , "(", $php );

 

$php = preg_replace( "#\r#" , "<br />", $php );

 

$php = preg_replace( "#\n#" , "<br />", $php );

 

$php = preg_replace( "#\s{1};#" , ";", $php );

 

// Ensure that spacing is preserved

 

$php = preg_replace( "#\s{2}#", " ", $php );

 

//fix stupid line break bug;

 

$php = preg_replace("#^\<font color=\"\#000000\">\<br />#", "", $php);

 

$php = preg_replace("#\<br />\</font>$#", "", $php);

 

$html = $this->wrap_style( 'php' );

 

return "<!--php1-->{$html['START']}<!--ephp1-->$php<!--php2-->{$html['END']}<!--ephp2-->";

 

}

Hope m^e and other enjoy the fruits of my labor to provide more to this community. Also I am still trying to see why there is a compatiblity issue with my really great table BB Code, but that is on the back burner. Find me at PHP Builder or Dev Shed as a regular contributor concerning PHP issues as Houdini on PHP Builder and Houdini2005 on Dev Shed. I have seen lonebyrd and XIII there before. Just had to get my day count back up above 60 so had to really contribute something worthwhile. Hope it helps others on their programming PHP and some systems like Invision.

 

Of yeah you will also have to add .phptop and .phpmain through the ACP to the main default CSS right after the .htmltop and .htmlmain and save it for this to work properly.

Edited by Houdini (see edit history)

Share this post


Link to post
Share on other sites

I have to say this is a superb piece of code, Houdini. Kudos all around :D.I'm not a very skilled PHP coder, but I know a good thing when I see it. And I'm sorry I couldn't test the code since I don't have an Invision board, but I can't see anything wrong with it. I'm sure the guys here will give it thorough testing, though.By the way, are you licensing this piece of code, or is it free for all?Anyway, thanks for a fantastic work.

Share this post


Link to post
Share on other sites

It is absolutely free to any that wish to use it! Gratis...no charge...use at own risk it should also work with newer version of Invision like on this site where all they did was add graphic buttons which I developed for 1.2 and 1.3 also and will post that code as well if asked.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.