Howdy...
I have several regular expressions questions... First of all, I am a Flash developer... I do not have good grasp of the PHP or regular expressions, but I am currently working on the bbCode tag for the phpBB software...
What I want to do is to create a custom bbCode that will read the Flash ActionScript and then generate the output with the predefined color set to display the reserved words and comments and such in different colors... Basically the [ PHP] tag for the Flash ActionScript it is...
I want the whole line to be colored in the color that is defined for the comment line...
This is how I see at the moment, (No actual BOLD face used in the example, but I have used it to show you where it happens...)// method. In doing so it increases the speed of
and this is how it should be...
// method. In doing so it increases the speed of
Some reserved words are not really detected...
For example, I have 'in' and 'int' as reserved words, yet the script just detects 'in' onliy in this case like this...Object.minorVersion = int(Object.version[2]);
I have defined my reserved words like this...
$as['reserved'] = array(
' add', ' and', ' break', ' call', ' case', ' continue', ' default', ' delete', ' do', ' else', ' elseif', ' eq', ' false', ' for', ' function', ' if', ' in', ' int', ' instanceof', ' new', ' newline', ' not', ' null', ' on', ' or', ' onClipEvent', ' return', ' set', ' super', ' switch', ' this', ' true', ' typeof', ' undefined', ' var', ' void', ' while', ' with' );Here for example, ' in'... As you can see, I have added ' '(space) in front of the keyword... If I omit it, I do get all the instances of 'in' to be displayed in green color like this...
built-[COLOR=green][b]in[/b][/color] [COLOR=green][b]in[/b][/color]creases conta[COLOR=green][b]in[/b][/color] plug[COLOR=green][b]in[/b][/color] m[COLOR=green][b]in[/b][/color]orVersion
You get the idea... So, my question is how to resolve this issue???
- I don't know why, but some reserved words are not really checked at all...
For example, this line displays...
but it actually is missing the first occurance of 'this' which should be displayed like this...this.firstChild.removeNode();
var treePtr = this;this.firstChild.removeNode();
var treePtr = this;
- This I have no idea yet, but I'd like to set a set of functions to be displayed in, say, red color... Those reserved words are sorta done, but I have no idea how to add additional words array to be checked...
This is the function that I have got at the moment...
/**
* AS MOD
* as_highlight_string($str)
* - All parameters required
* - Returns a string
* Takes a string and color codes it to AS's syntax.
* BEWARE! This CAN and WILL slow down page generation times! At least with large chunks of code.
* NOTE: The order of preg_replace's and str_replace's is key--DO NOT REORDER UNLESS YOU KNOW WHAT YOU'RE DOING
*/
function as_highlight_string($str)
{
$color['comment'] = '#FF9900';
$color['default'] = '#000000';
$color['reserved'] = '#009900';
$color['string'] = '#0000FF';
$as['reserved'] = array(
' add', ' and',
' break',
' call', ' case', 'c ontinue',
' default', ' delete', ' do',
' else', ' eq',
' false', ' for', ' function',
' if', ' in', ' int',
' instanceof',
' new', ' newline', ' not', ' null',
' on', ' or', ' onClipEvent',
' return',
' set', ' super', ' switch',
' this', ' true', ' typeof',
' undefined',
' var', ' void',
' while', ' with'
);
$str = preg_replace('/([\"])(.*?)([\"])/si', '<font color="' . $color['string'] . '">\\1\\2\\3</font>', $str);
for ($x = 0; $x < count($as['reserved']); $x++) {
$str = str_replace($as['reserved'][$x], '<font color="' . $color['reserved'] . '">' . $as['reserved'][$x] . '</font>', $str);
}
$str = preg_replace('[\/\*]', '<font color="' . $color['comment'] . '">\\1\\2\\3/*', $str);
$str = preg_replace('[\*\/]', '*/</font>', $str);
$str = preg_replace('/(\/\/)(.*?)([\n])/si', '<font color="' . $color['comment'] . '">\\1\\2\\3\\4</font>', $str);
$str = str_replace("\n", '<br />', $str);
$str = '<font color="' . $color['default'] . '">' . $str . '</font>';
return($str);
} // as_highlight_string()
/**
* AS MOD
* Does second-pass bbencoding of the [AS] tags. This includes
* running htmlspecialchars() over the text contained between
* any pair of [AS] tags that are at the first level of
* nesting. Tags at the first level of nesting are indicated
* by this format: [as:1:$uid] ... [/as:1:$uid]
* Other tags are in this format: [as:$uid] ... [/as:$uid]
*
* Original code/function by phpBB Group
* Modified by JW Frazier / Fubonis < [email]php@fubonis.com[/email] >
*/
function bbencode_second_pass_as($text, $uid, $bbcode_tpl)
{
global $lang;
$html_entities_match = array("#<#", "#>#");
$html_entities_replace = array("<", ">");
$code_start_html = $bbcode_tpl['as_open'];
$code_end_html = $bbcode_tpl['as_close'];
// First, do all the 1st-level matches. These need an htmlspecialchars() run,
// so they have to be handled differently.
$match_count = preg_match_all("#\[as:1:$uid\](.*?)\[/as:1:$uid\]#si", $text, $matches);
for ($i = 0; $i < $match_count; $i++)
{
$before_replace = $matches[1][$i];
$after_replace = $matches[1][$i];
$after_replace = preg_replace($html_entities_match, $html_entities_replace, $after_replace);
// Replace 2 spaces with " " so non-tabbed code indents without making huge long lines.
$after_replace = str_replace(" ", " ", $after_replace);
// now Replace 2 spaces with " " to catch odd #s of spaces.
$after_replace = str_replace(" ", " ", $after_replace);
// Replace tabs with " " so tabbed code indents sorta right without making huge long lines.
$after_replace = str_replace("\t", " ", $after_replace);
$str_to_match = "[as:1:$uid]" . $before_replace . "[/as:1:$uid]";
$replacement = $code_start_html;
$after_replace = as_highlight_string($after_replace);
$replacement .= $after_replace;
$replacement .= $code_end_html;
$text = str_replace($str_to_match, $replacement, $text);
}
// Now, do all the non-first-level matches. These are simple.
$text = str_replace("[as:$uid]", $code_start_html, $text);
$text = str_replace("[/as:$uid]", $code_end_html, $text);
return $text;
} // bbencode_second_pass_as()
To sum up, I think this all comes down to my lack of regular expression... It's been too long since I have used it so I hardly remember it... Anybody know any good online material for the RE???
The current script is implemented into the my test board which can be found at http://forum.flashvacuum.com and the specific thread that shows the [AS] tag example is http://forum.flashvacuum.com/viewtopic.php?t=201
You can use 'test/test' to login and test the script if you want to...
Any help is very appreciated... Thanks for your time to read this...
Jason