Jump to content

A New [code]


Irap

Recommended Posts

Maybe I can finally fix this here.

There's a coding language that has it's own code-like tag on my forum. Most of the code works, but there are many bugs with it. I can't quite figure out how to do numbers either.

The main problem is, when I use PHP to and <span> HTML, the HTML often becomes part of the search.

I've been using preg_replace, not preg_match (I find it a little more confusing). Maybe that's the problem...

Another problem is when certain parts match twice for different things. I need a way to tell the difference of which to do.

[code=php:0]// HEX Numbers.
$code = preg_replace( '#\b(0x([0-9a-fA-F]+))#i', "<span class='code-number'>\\1</span>", $code );

// Numbers *broked* - doesn't replace always, sometimes displays HTML.
// $code = preg_replace( '#\b([0-9.]+)\b#', "<span class='code-number'>\\1</span>", $code );

// special variables (12@)
// Want this to be compatible with arrays 12@(4,25int)
$code = preg_replace( "#(([0-9]+)@)#i", "<span class='code-variable'>\\1</span>", $code );

// Variables ($var)
// Want this to be compatible with arrays $var[23] $var(23,25int)
$code = preg_replace( '#((\$|\&)([a-zA-Z0-9_]+))#', '<span class=\'code-variable\'>\\1</span>', $code );

// Label or Label pointer (@label - :label)
$code = preg_replace( "#((@|pzcolon)([a-zA-Z0-9_]+))#i", "<strong class='code-lable'>\\1</strong>" , $code );

// line comment - bug: changes colour within the comment ("text" would be red).
$code = preg_replace( "#(//(.*?)\n)#i", "<span class='code-comment'>\\1</span>" , $code );

// block comment - bug: doesn't change if 

is reached before \n.

$code = preg_replace( "#({.*?})#is", "<span class='code-comment'>\\1</span>" , $code );

 

// Strings ("") - seems to work smoothly

$code = preg_replace( '#("(.*?)")#', '<span class=\'code-string\'>\\1</span>' , $code );

 

// #TEXT - #421numbers29

$code = preg_replace( "#(\#([a-zA-Z0-9_]+))#i", "<strong class='code-number'>\\1</strong>", $code );

 

// Replace HTML ' with " so that the string replace can be performed safely.

$code = preg_replace( "#(class='code-string')#", "class=\"code-string\"" , $code );

$code = preg_replace( "#(class='code-comment')#", "class=\"code-comment\"" , $code );

$code = preg_replace( "#(class='code-lable')#", "class=\"code-lable\"" , $code );

$code = preg_replace( "#(class='code-number')#", "class=\"code-number\"" , $code );

$code = preg_replace( "#(class='code-variable')#", "class=\"code-variable\"" , $code );

 

// Strings ('')

$code = preg_replace( "#(\'(.*?)\')#", "<span class=\"code-string\">\\1</span>" , $code );

 

// Keywords

$code = preg_replace( "#\b(if|and|wait|return|downto|else|end|false|true|for|hex|not|or|repeat|then|to|unknown|var|until|while)\b#i", "<strong class='code-keyword'>\\1</strong>" , $code );

$code = preg_replace( "#\b(classword|classword2|myclassword|anotherclassword)\.\b#i", "<strong class='code-class'>\\1</strong>." , $code );

$code = preg_replace( "#\.\b(commandword|commandword2|mycommandword|anothercommandword)\b#i", ".<strong class='code-command'>\\1</strong>" , $code );[/code]

Link to comment
https://forums.phpfreaks.com/topic/194426-a-new-code/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

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.