Enjoy an ad free experience by logging in. Not a member yet?
Register .
10-16-2009, 06:39 PM
PM User |
#1
Regular Coder
Join Date: Jan 2009
Posts: 153
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 07:46 PM ..
10-16-2009, 06:47 PM
PM User |
#2
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 );
10-16-2009, 07:07 PM
PM User |
#3
Regular Coder
Join Date: Jan 2009
Posts: 153
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 } ?>
10-16-2009, 07:13 PM
PM User |
#4
Regular Coder
Join Date: Oct 2008
Location: Near Montreal, Quebec - Canada.
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 07:17 PM ..
Users who have thanked AlexV for this post:
10-16-2009, 07:45 PM
PM User |
#5
Regular Coder
Join Date: Jan 2009
Posts: 153
Thanks: 47
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by
TheWolf1
Use:
PHP Code:
echo( str_replace (array( '+' , '-' ), array( '<img src="plus.gif" alt="+">' , '<img src="minus.gif" alt="-">' ), $contents [ 4 ]));
Works great, thx guys!
Jump To Top of Thread
Thread Tools
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
HTML code is Off
All times are GMT +1. The time now is 02:53 AM .
Advertisement
Log in to turn off these ads.