473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 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 2966
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.