RE: [PHP] RegEx (back referencing)

From: Date: Mon, 05 Aug 2002 12:49:54 +0000
Subject: RE: [PHP] RegEx (back referencing)
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-111201@lists.php.net to get a copy of this message
Dan, Thanks for your explanation. In fact Danny's version of using \\0 did the trick without altering the pattern, useful to now about the ' chars making faster processing though. My understanding of back referencing was that it is the ability to use all or part of a search pattern in the replace string, if this is wrong I think I need to read up again, can you recommend any good reference manuals (preferably not specific to a single language construct, I mainly use ColdFusion in my professional capacity and like to convert my CF scripts to PHP). I have another regex question now, perhaps you could help. Is it possible to give a start position within the string being searched. I want to find x occurences of a pattern, then use a regular expression to search only a given section of the whole string. This is probably because my regex is not that great and I cannot work out how to find an instance of a pattern only if it is contained in another pattern. Here's my problem... When converting JS comments & strings, it is possible certain chars already have font/bold tags wrapped around them and need to be removed. // comments example $nl = chr(10); $fcontent = eregi_replace("[^http:]/{2,2}([^$nl]*)$nl", "«font color=darkgreen»«i»\\0«/i»«/font»", $fcontent); I now want to remove any font/bold opening/closing tags from inside the string(s) «font color=darkgreen»«i» «/i»«/font». http://www.n-igma.net/regex.php The above shows what happens to strings and comments with chars that are being used in other search patterns. TIA Phil. P.S - I fully agree with you regarding font colors over CSS, I am using font tags here while the script is being perfected before rolling out the final version ;o) > -----Original Message----- > From: Analysis & Solutions [mailto:danielc@analysisandsolutions.com] > Sent: 04 August 2002 19:34 > To: PHP List > Subject: Re: [PHP] RegEx (back referencing) > > > 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 >

« previous php.general (#111201) next »