...

View Full Version : Resolved Need help with str_replace



Daniellez
10-16-2009, 06:39 PM
So far, I have the following:


<?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. :(

met
10-16-2009, 06:47 PM
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


// 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);

Daniellez
10-16-2009, 07:07 PM
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
...
$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
}
?>

AlexV
10-16-2009, 07:13 PM
Use:


echo(str_replace(array('+', '-'), array('<img src="plus.gif" alt="+">', '<img src="minus.gif" alt="-">'), $contents[4]));

Daniellez
10-16-2009, 07:45 PM
Use:


echo(str_replace(array('+', '-'), array('<img src="plus.gif" alt="+">', '<img src="minus.gif" alt="-">'), $contents[4]));

Works great, thx guys!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum