Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    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[8]))); ?>

  • #2
    God Emperor Fou-Lu's Avatar
    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:
    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 %.


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts