473,787 Members | 2,857 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2994
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($lis t), $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($pric e_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($lis t), $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($pric e_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($lis t), $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
4682
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 parentheses at offset 2 I have looked in the manual but no luck so far
5
2117
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
10159
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 there better ways to remove extra new lines???
3
4321
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' which may contain new lines and will contain html code, IN this string any "words" that begin with an underscore I want to replace with a given word. A word here being a group of chars preceded by a space or null (start of line) and closed by a...
1
2243
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 output i would expect is as follows:
7
4999
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 and, after submitting via POST to a preg_replace filter, get back '_sometext_' (i.e., the same thing with the literal backslashes stripped)
3
5326
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 --- Sig line1 Sig line2 Sig line3 If I could just get rid of that, it would be pretty good. But I also get this
16
2893
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*?>", "", $html); Unfortuntely it is not working and giving the following error:
8
2373
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 or quotation marks around them as well as lines that start with a hyphen. Lastly the lines with ** need the brackets before the **, instead of at the end of the line. Here is what I have so far - it is close, but I keep ending up with
0
9655
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10169
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9964
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7517
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.