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-16-2009, 06:39 PM   PM User | #1
Daniellez
Regular Coder

 
Join Date: Jan 2009
Posts: 153
Thanks: 47
Thanked 0 Times in 0 Posts
Daniellez is on a distinguished road
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..
Daniellez is offline   Reply With Quote
Old 10-16-2009, 06:47 PM   PM User | #2
met
Regular Coder

 
Join Date: Oct 2009
Location: United Kingdom
Posts: 728
Thanks: 4
Thanked 119 Times in 119 Posts
met has a little shameless behaviour in the past
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); 
met is offline   Reply With Quote
Old 10-16-2009, 07:07 PM   PM User | #3
Daniellez
Regular Coder

 
Join Date: Jan 2009
Posts: 153
Thanks: 47
Thanked 0 Times in 0 Posts
Daniellez is on a distinguished road
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
}
?>
Daniellez is offline   Reply With Quote
Old 10-16-2009, 07:13 PM   PM User | #4
AlexV
Regular Coder

 
AlexV's Avatar
 
Join Date: Oct 2008
Location: Near Montreal, Quebec - Canada.
Posts: 214
Thanks: 5
Thanked 22 Times in 22 Posts
AlexV is an unknown quantity at this point
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..
AlexV is offline   Reply With Quote
Users who have thanked AlexV for this post:
Daniellez (10-16-2009)
Old 10-16-2009, 07:45 PM   PM User | #5
Daniellez
Regular Coder

 
Join Date: Jan 2009
Posts: 153
Thanks: 47
Thanked 0 Times in 0 Posts
Daniellez is on a distinguished road
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!
Daniellez 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 02:53 AM.


Advertisement
Log in to turn off these ads.