Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-04-2012, 07:51 PM   PM User | #1
phpchick
New Coder

 
Join Date: May 2011
Location: new york
Posts: 93
Thanks: 4
Thanked 0 Times in 0 Posts
phpchick is an unknown quantity at this point
3 nested str_replace statements, more efficient method?

I'm doing this currently but I imagine there has to be a better way of doing it?


<?php echo str_replace("+", "", str_replace("%", "%)", str_replace('"', '', $stock[8]))); ?>
phpchick is offline   Reply With Quote
Old 10-04-2012, 08:16 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,750
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Unfortunately that depends entirely on what you are wanting to do.
String replacement is much better performance wise the pattern matching. However, str_replace in sequence will always apply ltr, so first you replace " with nothing, then you replace % with %), then you replace + with nothing. In this particular situation, you can likely get away with a simple:
PHP Code:
echo str_replace(array('+''%''"'), array('''%)'''), $stock[8]); 
The thing to be careful with with str_replace is that any replacement you do doesn't impact on the next set (unless that is the intent of course). So if I were to replace + with '%' and '%' with '%)', then every + would become %) when the intent may be to replace it with %.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:51 PM.


Advertisement
Log in to turn off these ads.