Re: preg_replace() in 4.0.4

From: Date: Mon, 08 Jan 2001 12:36:40 +0000
Subject: Re: preg_replace() in 4.0.4
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-33213@lists.php.net to get a copy of this message
G'day Monte (and list), My original query was: >> With the upgrade to 4.0.4, preg_replace() now does substitutions for $n in >> the replacement strings (I say strings, because it's the easiest way of >> doing mass substitutions over an array). This has caused me severe grief >> (as there doesn't appear to be any way to stop it). >> >> I have an application which reads in a PostScript template file and >> replaces occurrences of '@' delimited strings with specific values >> (basically, a "variable subsituter"). Some of the replacement strings may >> contain dollar amounts and so a '$' may (or may not) be present. If a >> '$' >> is present in a string, it will be followed by a digit. When I call >> preg_replace() in this application, all 3 arguments are arrays. >> ...[text deleted]... To which Monte replied: > escape the $ signs in the replace array first, so $1 becomes \$1, etc. The problem with that is that you then end up with a "\$1" literally in the final array. That may be fine if you're just doing something purely within PHP, but in this case the data is being written as part of a PostScript output stream. The actual code is something along the lines of: $pat = array('@v1@', '@v2@', ... '@vn@'); // Constant strings $rplc = array($a, $b, $c, 1, 1, ..., $n); $new = preg_replace ($orig, $rplc, $tmplt); If, for example, $b contains '$1,234.56' the replacement for '@v2@' becomes ',234.56'. If the '$' is escaped, the replacement is literally '\$1,234.56'. To complicate things further, $b may not be a "dollar value", but text along the lines of 'N/A'. In my case, I don't actually want to do regular-expression substitution but straightforward text substitution. But, as the arguments are all arrays (lots of "variables" to be replaced by "values" in a pre-processed template), I can't just use str_replace(). Any suggestions would be much appreciated. Thanks. -- -------------------------------------------------------+--------------------- Daniel Baldoni BAppSc, PGradDipCompSci | Technical Director require 'std/disclaimer.pl' | LcdS Pty. Ltd. -------------------------------------------------------+ 856B Canning Hwy Phone/FAX: +61-8-9364-8171 | Applecross Mobile: 041-888-9794 | WA 6153 URL: http://www.lcds.com.au/ | Australia -------------------------------------------------------+--------------------- "Any time there's something so ridiculous that no rational systems programmer would even consider trying it, they send for me."; paraphrased from "King Of The Murgos" by David Eddings. (I'm not good, just crazy)

« previous php.general (#33213) next »