Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
10-04-2012, 07:51 PM #1
- Join Date
- May 2011
- new york
- Thanked 0 Times in 0 Posts
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))); ?>
10-04-2012, 08:16 PM #2
- Join Date
- Sep 2002
- Saskatoon, Saskatchewan
- Thanked 2,660 Times in 2,629 Posts
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:
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 %.PHP Code:
echo str_replace(array('+', '%', '"'), array('', '%)', ''), $stock);