On Sat, Aug 03, 2002 at 05:03:36PM +0100, Phil Ewington wrote: > Hi, > > I am am writing a function to color code and indent JavaScript source using > regular expressions and cannot seem to get back referencing working.
What you're using isn't "back referencing," it's utilizing "substrings." Back referencing is when you bring the substrings back into the regular expression definition. > $string = eregi_replace("<(/?)(scr[^>]*)>", "«font color=maroon»\1«/font»", > $string); Here's what your command asks PHP to do: Find any opening or closing script tag and replace it with "«font color=maroon»\1«/font»." That's not what you intend. Let's make a test script: <?php $string = 'begin <script>java is dumb</script> end'; $string = eregi_replace("<(/?)(scr[^>]*)>", "«font color=maroon»\1«/font»", $string); echo "$string\n"; ?> This is what comes out: begin «font color=maroon»«/font»java is dumb«font color=maroon»«/font» end First off, if you want to utilize substrings, you need to use two backslashes. "\\1" instead of "\1". Second, \\1 refers to the first parenthesized part of the pattern, which in the pattern you set up is the forward slash at the beginning of the script tag. So, let's modify the pattern accordingly and see what happens: $string = eregi_replace("<(/?)(scr[^>]*)>", "«font color=maroon»\\1«/font»", $string); Creates: begin «font color=maroon»«/font»java is dumb«font color=maroon»/«/font» end But, that's still not what you intend. I think you're really looking to surround the scripts with maroon font tags. To do that, you need to tweak the expression to grab the script tags, the script itself and the closing script tag. $string = eregi_replace('(<script[^>]*>[^<]*</script>)', '<font color="maroon">\\1</font>', $string); Outputs: begin <font color="maroon"><script>java is dumb</script></font> end Notice the use of ' rather than ", which saves a little processor time by not evaluaing the strings for variables. One final improvement. Font tags are obnoxious. They cause problems for people that choose their own background colors in their browsers. Read http://www.analysisandsolutions.com/code/weberror.htm?ft=y for more info on that. Anyway, I suggest using Cascading Style Sheets, http://www.w3.org/TR/REC-CSS1, for the colroing instead: $string = eregi_replace('(<script[^>]*>[^<]*</script>)', '<code class="js">\\1</code>', $string); echo "<style>code.js {color: maroon;}</style>\n"; echo "$string\n"; Yields: <style>code.js {color: maroon;}</style> begin <code class="js"><script>java is dumb</script></code> end Voila! --Dan -- PHP classes that make web design easier SQL Solution | Layout Solution | Form Solution sqlsolution.info | layoutsolution.info | formsolution.info T H E A N A L Y S I S A N D S O L U T I O N S C O M P A N Y 4015 7 Av #4AJ, Brooklyn NY v: 718-854-0335 f: 718-854-0409 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php