Hi and I am expanding one of my websites which requires me to write a script which has a language syntax highlighter (the gml/gamemaker language). So to do that I am using mainly preg replace and str replace. I have been working hard at it for the last 29 hours but there is just three bugs (or preg replace functions) which is messing up the end result. I have attached to this post the code (as an attachment) since several phrases in my code kept on getting replaced with the bbcode <code> tag but with the [] brackets.

The 3 preg replaces which are sending me crazy are as follows:

$textb=preg_replace("/([^)])( |
|	|\>| \>|
\>|	\>)(.*)\((.*)?\)([^(])?()/is",'$1$2<font color=#c#o#l#o#r#2#>$3($4)</font>$5',$textb);

// one preg replace that works was taken out from this spot.
		
$textb=preg_replace("/(\'(.*)\')/is","<font color=#c#o#l#o#r#3#>'$2'</font>",$textb);
$textb=preg_replace("/(\'([^'])\')/is","'</font>$2<font color=#c#o#l#o#r#3#>'",$textb);

You will find that above code in the attachment file with its surrounding code but it is those lines of code that are messing up the final result (I believe). Currently the below text is replace by the html below that. But please replace the brackets <> with the brackets [].

This is a test line
<code>
	 function('value1','value2','value3');
	 functionb("value1","value2",value3);
	functionb("value1","value2",value3);
globalvar variable;
</code>
This is more text

The above is preg/str replaced with the below currently.

This is a test line<br>
<font ><font color=#000080>color=#000000><br>
	 function(<font color=#800000>'value1'</font>,<font color=#800000>'value2'</font>,<font color=#800000>'value3'</font>);<br>
	 functionb(<font color=#800000>"value1"</font>,<font color=#800000>"value2",value3);<br>
	 functionb("value1"</font>,<font color=#800000>"value2"</font>,value3)</font>;<br>
<b>globalvar</b> variable;<br>
</font><br>
This is more text

As you can see in the above box, there are a few final result bugs all of which I believe come from the php code in the first code box I posted. Could anyone help me with programming the preg syntax as I have used up all my ideas and no-longer have a clue on what to do for this problem.

Note: If you want all the code just download the attachment to this post as the bbcode syntax got mixed up with my php code due to the function: <code> but with the [] brackets.

Attachments
<?
// The below variable gets used in inside the first appearance
// of the $text variable and is used to show the quotation marks
// in a different way.
$moretext=' functionb("value1","value2",value3);';

// The below variable is the input text.
$text="This is a test line
[code]
	 function('value1','value2','value3');
	".' functionb("value1","value2",value3);'."
	".$moretext."
globalvar variable;
[/code]
This is more text";
// The above variable was the input text.

// Below splits the document into code and text sections
$text=str_replace('[code]','[<-code->][code]',$text);
$text=preg_split('/\[\<\-code\-\>\]/',$text);


// The below 2 lines initialises the variables for the filter
// cycle/while loop of the gml syntax highlighter.
$arrayval=0;
$textc='';

// The below loop checks each individual split from above.
while (isset($text[$arrayval]))
	{
	// The below 3 lines check if the current split is a code
	$textb=$text[$arrayval];
	$ifvar=@substr_replace($textb,'',6,strlen($textb));
	if ($ifvar=='[code]')
		{
		// If it is a code, then the below replacements will happen.
		$textb=preg_replace('/
/is','<br>',$textb);
		$textb=str_replace('[code]','<font color=#c#o#l#o#r#1#>',$textb);
		$textb=str_replace('[/code]','</font>',$textb);
		$textb=preg_replace("/([^)])( |
|	|\>| \>|
\>|	\>)(.*)\((.*)?\)([^(])?()/is",'$1$2<font color=#c#o#l#o#r#2#>$3($4)</font>$5',$textb);

		$textb=str_replace('>	<','><',$textb);
		
		$textb=preg_replace("/(\'(.*)\')/is","<font color=#c#o#l#o#r#3#>'$2'</font>",$textb);
		$textb=preg_replace("/(\'([^'])\')/is","'</font>$2<font color=#c#o#l#o#r#3#>'",$textb);


		$textb=preg_replace('/(\"(.*)\")/is','<font color=#c#o#l#o#r#3#>"$2"</font>',$textb);
		$textb=preg_replace('/(\"([^"])\")/is','"</font>$2<font color=#c#o#l#o#r#3#>"',$textb);		
		
		$textb=str_replace('globalvar','<b>globalvar</b>',$textb);
		$textb=preg_replace('/( |
|	|\>| \>|
\>|	\>)var /is','$1<b>var </b>',$textb);
		
		

		// Then the split is re-attached to a variable
		$textc=$textc.$textb;
		} else {
		
		// If it is not code, the below 2 replacements will be made.
		$textb=preg_replace('/
/is','<br>',$textb);

		// Then the split is re-attached to a variable.
		$textc=$textc.$textb;
		}
	// The below line sets up the next split array.
	$arrayval+=1;
	}

// When all of the splits have been processed and remerged as the
// $textc variable, the below replacements are made.

$textc=@str_replace('#c#o#l#o#r#1#','#000000',$textc);
$textc=@str_replace('#c#o#l#o#r#2#','#000080',$textc);
$textc=@str_replace('#c#o#l#o#r#3#','#800000',$textc);

// Then to view the results, the variable $textc is being echo'd.
echo $textc;

?>

My biggest recommendation would be to use a prebuilt syntax highlighter and just add a syntax extension. GeSHi is one my my favorites.

My biggest recommendation would be to use a prebuilt syntax highlighter and just add a syntax extension. Geshi is one my my favorites.

Thanks for that reply and Geshi is doing the job perfectly. So the code below I replaced with the code provided by Geshi (keeping in mind Geshi uses other files) with a few other minor changes to the attachment in the first post.

$textb=preg_replace("/([^)])( |
|	|\>| \>|
\>|	\>)(.*)\((.*)?\)([^(])?()/is",'$1$2<font color=#c#o#l#o#r#2#>$3($4)</font>$5',$textb);

		$textb=str_replace('>	<','><',$textb);
		
		$textb=preg_replace("/(\'(.*)\')/is","<font color=#c#o#l#o#r#3#>'$2'</font>",$textb);
		$textb=preg_replace("/(\'([^'])\')/is","'</font>$2<font color=#c#o#l#o#r#3#>'",$textb);


		$textb=preg_replace('/(\"(.*)\")/is','<font color=#c#o#l#o#r#3#>"$2"</font>',$textb);
		$textb=preg_replace('/(\"([^"])\")/is','"</font>$2<font color=#c#o#l#o#r#3#>"',$textb);		
		
		$textb=str_replace('globalvar','<b>globalvar</b>',$textb);
		$textb=preg_replace('/( |
|	|\>| \>|
\>|	\>)var /is','$1<b>var </b>',$textb);

So if you check the manual of the version of Geshi you have, the code provided to view the source in the syntax highlighter replaced that code.

So thanks for that reply as it has solved my question and I love how Geshi supports about 106 types of syntax's.

*Solved*

This question has already been answered. Start a new discussion instead.