472,958 Members | 2,277 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

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

Jul 17 '05 #1
3 2942
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Stijn Goris | last post by:
Hi all, I m trying to create a smiley system but no luck I have preg_replace ("/:-)/", '<img src="images/smiley.gif">', $strings); but it returns Warning: Compilation failed: unmatched...
5
by: paul brown | last post by:
howdy, I have some text: This is {search}Pam Grier{/search}. What I want is this: This is <a href="somepage.php?keyword=Pam Grier">Pam Grier</a>.
4
by: Sidharta | last post by:
Hi all, how come this doesn't work????? # convert to unix new lines $text = preg_replace("/\r\n/", "\n", $text); # remove extra new lines $text = preg_replace("/\n+/", "\n", $text); is...
3
by: TXSherry | last post by:
Hi, I cannot seem to wrap my brain around preg_replace. Though I've read the help file backwords and forwards. :/ Hoping someone can give me a solution here. Problem: Given string 'str'...
1
by: yawnmoth | last post by:
say i have the following script: <? $test = "aaaaa"; print '"' . preg_replace('/.*/','x',$test) . '"<br>'; $test = "\n\n\n\n\n"; print '"' . preg_replace('/.*/','x',$test) . '"'; ?> the...
7
by: Margaret MacDonald | last post by:
I've been going mad trying to figure out how to do this--it should be easy! Allow the user to enter '\_sometext\_', i.e., literal backslash, underscore, some text, literal backslash, underscore...
3
by: Charles | last post by:
I'm new to this regular expression stuff. I'd like to use preg_replace to eliminate a known multi-line signature from the body of an E-mail. Say the body text is in $body, and the sig is this ...
16
by: bissatch | last post by:
Hi, I am trying to use preg_replace to take out all occurances of PHP code after reading (fread()) the contents of a PHP file. The code I am using is: $html = preg_replace("<?php*?>", "",...
8
by: erikcw | last post by:
Hi all, I'm trying to write a regex pattern to use in preg_replace. Basically I want to put around every line (\n) in this variable. However, I need to exclude lines that already have brackets...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.