Page 1 of 1

Filters and highlighting source

Posted: Tue Jan 21, 2003 8:38 am
by BDKR
I'd like to know what was done on this board to deal with issues surrounding the
highlighting of php code. At my web log / rant site, I'm creating / have created a couple
of features to help facilitate rantings, addition of links (dictionary and jargon filters), a bad word filter, and now, a filter to highlight included code. It works great except for the issue of double and single quotes. This isn't an issue where add and strip slashes is going to do any good.

Anyways, this board does a wonderful job of it, does anyone know what it's doing?

Here is mine.

Code: Select all

<?php
# The idea of this filter is to take some <php> tags and replace them with highlighted code in a table
function php_filter($string)
  {
  $table_open="<table border="0" width="70%"><tr><td bgcolor="white"><?\n";
  $table_close="\n?></td></tr></table>";    
  $str_array1=explode("\n", $string);												
  unset($string);																
  $new_string='';															
  $x=sizeof($str_array1);														
  $y=0;																		
  $write_flag='s';																
  $buffer=array();															
  $n=0;
  for($y=0; $y<$x; ++$y)
	{
	if(strstr($str_array1[$y], '<php>'))
	  { $write_flag='y'; }

	if($write_flag=='y')  
	  { @$buffer[$n].=$str_array1[$y]."\n"; }										
	
	if(strstr($str_array1[$y], '</php>'))
	  {
	  $buffer[$n]=str_replace('<php>', $table_open, $buffer[$n]);						
  	  $buffer[$n]=str_replace('</php>', $table_close, $buffer[$n]);
	  $buffer[$n]=@highlight_string($buffer[$n], true);								
          $buffer[$n]=str_replace("<code><font color="#000000">", "", $buffer[$n]);		
	  $buffer[$n]=str_replace("</code>", "", $buffer[$n]);	
  	  $buffer[$n]=str_replace("<", "<", $buffer[$n]);
	  $buffer[$n]=str_replace(">", ">", $buffer[$n]);	  

	  $write_flag='append'; 													
	  }
	  
	if($write_flag=='n' || $write_flag=='s')										
	  { $new_string.=$str_array1[$y]; }
	elseif($write_flag=='append')
	  { 
	  $new_string.=$buffer[$n];
	  $write_flag='n';
	  ++$n;
	  }
	}
  return $new_string;	
  }
?>
How it works is to build an array of a string and rebuild it (the string) with the highlighted stuff inserted. It all works except for the fact that things get terribly messed up concerning double and single quotes.

The function can probably be optimized or re-thought in certain areas, but for now, I need to get this issue sussed out.

So if anyone has had any experience with this, let me know. I would really appreciate it.

Cheers,
BDKR

Posted: Tue Jan 21, 2003 9:32 am
by twigletmac
You can check out the PHP mod for PHPBB here:
http://www.phpbb.com/mods/downloads/upl ... bbcode.txt

Contains the function that the board uses to do this stuff. Which reminds me I need to stop it changing && to &&...

Mac

Posted: Tue Jan 21, 2003 9:36 am
by BDKR
Thanx Twig! More proof that you are worth your wieght in gold.

Cheers,
BDKR

Posted: Tue Jan 21, 2003 12:34 pm
by BDKR
Hey Twig,

FYI, the problem was figured out. I fixed it by adding these two lines. One just before, and the other just after the highlight_string() function

Code: Select all

<?php
$buffer[$n]=str_replace('\'', '\\\'', $buffer[$n]);	      // Prep
$buffer[$n]=@highlight_string($buffer[$n], true);     // Highlight the code
$buffer[$n]=str_replace('\\\'', '\'', $buffer[$n]); 	      // Cleanup work	
?>
However, it was still nice to see the approach that someone else took to solving the problem. I compared the two and noticed that his is a lot more flexible. On the other hand, mine is intened to be used by me more as opposed to just the casual commenter. Also, I had started with preg_match_all(), but dropped it when I was having a ton of issues getting it to catch a match across line breaks. It was good to see how he did it.

Oh well....

Cheers,
BDKR