By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,269 Members | 1,519 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,269 IT Pros & Developers. It's quick & easy.

preg_replace and literal $

P: n/a
I am replacing a string in a text block that has a literal $ in it, and
preg_replace is seeing it as a backreference. Here is what I am using:

foreach($price_lists as $list)
$x=preg_replace('/--PRICE-LIST--/',$list,$x,1);

OK, so what this does it is takes each array element and replaces only
the first occurrance of "--PRICE-LIST--" with it. I would have used
str_replace, but I didn't think it should be necessary to create an
array with the same number of elements as $price_list just for a simple
thing like this.

I did, hoever try:
$x=str_replace('--PRICE-LIST--',$price_lists,$x);

Since the manual page didn't say anything about that case to no avail.

So right now I have the following:
foreach($price_lists as $list)
$x=preg_replace('/--PRICE-LIST--/',str_replace('$','\$',$list),$x,1);

Which compensates for this. However, that is just one case, I cane
forsee others showing up. Does anyone know of a way to make preg_replace
behave as I intend it to? Or does someone have a function for regex-safe
string encoding?

TIA!

--
Justin Koivisto - sp**@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.

Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Justin Koivisto <sp**@koivi.com> writes:
So right now I have the following:
foreach($price_lists as $list)
$x=preg_replace('/--PRICE-LIST--/',str_replace('$','\$',$list),$x,1);

Which compensates for this. However, that is just one case, I cane forsee
others showing up. Does anyone know of a way to make preg_replace behave as
I intend it to? Or does someone have a function for regex-safe string
encoding?


Change it to:
foreach($price_lists as $list)
$x=preg_replace('/--PRICE-LIST--/', preg_quote($list), $x, 1);

And of course, see <http://us2.php.net/preg_quote>

HTH,
--
steven vasilogianis
Jul 17 '05 #2

P: n/a
Steven Vasilogianis wrote:
Justin Koivisto <sp**@koivi.com> writes:

So right now I have the following:
foreach($price_lists as $list)
$x=preg_replace('/--PRICE-LIST--/',str_replace('$','\$',$list),$x,1);

Which compensates for this. However, that is just one case, I cane forsee
others showing up. Does anyone know of a way to make preg_replace behave as
I intend it to? Or does someone have a function for regex-safe string
encoding?

Change it to:
foreach($price_lists as $list)
$x=preg_replace('/--PRICE-LIST--/', preg_quote($list), $x, 1);


Thanks! I'm surprized that I didn't see that one.

--
Justin Koivisto - sp**@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.

Jul 17 '05 #3

P: n/a
Steven Vasilogianis wrote:
Justin Koivisto <sp**@koivi.com> writes:

So right now I have the following:
foreach($price_lists as $list)
$x=preg_replace('/--PRICE-LIST--/',str_replace('$','\$',$list),$x,1);

Which compensates for this. However, that is just one case, I cane forsee
others showing up. Does anyone know of a way to make preg_replace behave as
I intend it to? Or does someone have a function for regex-safe string
encoding?


Change it to:
foreach($price_lists as $list)
$x=preg_replace('/--PRICE-LIST--/', preg_quote($list), $x, 1);

And of course, see <http://us2.php.net/preg_quote>


Unfortunately, that didn't work for my application because there is HTML
in it, and it escaped all the < and >. However, After reviewing the
manual page, it looks like I should be OK with only escaping the $ in
the strings.

--
Justin Koivisto - sp**@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.