preg_replace and literal $

This is a discussion on preg_replace and literal $ within the PHP Language forums, part of the PHP Programming Forums category; I am replacing a string in a text block that has a literal $ in it, and preg_replace is seeing it ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-16-2003
Justin Koivisto
 
Posts: n/a
Default preg_replace and literal $

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 - spam@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.

Reply With Quote
  #2 (permalink)  
Old 10-17-2003
Steven Vasilogianis
 
Posts: n/a
Default Re: preg_replace and literal $

Justin Koivisto <spam@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
Reply With Quote
  #3 (permalink)  
Old 10-17-2003
Justin Koivisto
 
Posts: n/a
Default Re: preg_replace and literal $

Steven Vasilogianis wrote:

> Justin Koivisto <spam@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 - spam@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.

Reply With Quote
  #4 (permalink)  
Old 10-17-2003
Justin Koivisto
 
Posts: n/a
Default Re: preg_replace and literal $

Steven Vasilogianis wrote:

> Justin Koivisto <spam@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 - spam@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 06:45 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0