Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jan 2009
    Posts
    156
    Thanks
    47
    Thanked 0 Times in 0 Posts

    Need help with str_replace

    So far, I have the following:

    PHP Code:
    <?php echo str_replace("-","<img src=minus.gif>","$contents[4]"?>
    In addition to the above, if there is a + in $contents[4], I'd like to also replace the + with <img src=plus.gif>

    I just can't figure out how to use str_replace to look for and replace two different characters in the same string. :(
    Last edited by Daniellez; 10-16-2009 at 08:46 PM.

  • #2
    met
    met is offline
    Regular Coder
    Join Date
    Oct 2009
    Location
    United Kingdom
    Posts
    728
    Thanks
    4
    Thanked 119 Times in 119 Posts
    pass it an array of characters instead of a single one

    i'm lazy and just copied/pasted from http://php.net/manual/en/function.str-replace.php
    PHP Code:
    // Provides: You should eat pizza, beer, and ice cream every day
    $phrase  "You should eat fruits, vegetables, and fiber every day.";
    $healthy = array("fruits""vegetables""fiber");
    $yummy   = array("pizza""beer""ice cream");

    $newphrase str_replace($healthy$yummy$phrase); 

  • #3
    Regular Coder
    Join Date
    Jan 2009
    Posts
    156
    Thanks
    47
    Thanked 0 Times in 0 Posts
    Hmm, not sure why this isn't working (the + and - characters are being displayed rather than the gifs). I used the example but obviously I've messed it up. Any idea what's wrong with this?

    PHP Code:
    <?php
    ...
    $contents explode(','str_replace('$'''$output));
    $char = array("-""+");
    $icon = array("<img src=minus.gif>""<img src=plus.gif>");
    {
    ?>
    <?php
    ?>

    <?php echo str_replace("$char","$icon","$contents[4]"?>

    <?php
    }
    ?>

  • #4
    Regular Coder
    Join Date
    Oct 2008
    Posts
    214
    Thanks
    5
    Thanked 22 Times in 22 Posts
    Use:

    PHP Code:
    echo(str_replace(array('+''-'), array('<img src="plus.gif" alt="+">''<img src="minus.gif" alt="-">'), $contents[4])); 
    Last edited by AlexV; 10-16-2009 at 08:17 PM.

  • Users who have thanked AlexV for this post:

    Daniellez (10-16-2009)

  • #5
    Regular Coder
    Join Date
    Jan 2009
    Posts
    156
    Thanks
    47
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by TheWolf1 View Post
    Use:

    PHP Code:
    echo(str_replace(array('+''-'), array('<img src="plus.gif" alt="+">''<img src="minus.gif" alt="-">'), $contents[4])); 
    Works great, thx guys!


  •  

    Posting Permissions

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