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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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. |
|
|||
|
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 |
|
|||
|
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. |
|
|||
|
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. |