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.

Recommended Answers

All 2 Replies

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*

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.