Filters and highlighting source

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Filters and highlighting source

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

Thanx Twig! More proof that you are worth your wieght in gold.

Cheers,
BDKR
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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
Post Reply