PHP Search and Replace in String Array

So i have an array, you can see in the code below, what i want to do is parse through it and replace various characters different characters, I have my function you can see to do this, it works but only executes the last str_replace, it doesnt do all of them  Below the function i call the text as

<?=$text["Title"]?>

so when it comes out it needs to be clean and replaced by all of the str_replaced not just the last one.
<?php

$text = array(

	"Title" => "Hello & World!",
	"Heading" => "I’m",
	"Bottom" => "I hate & ””People"
)
?>


<?php


foreach($text as $key=>$value) {
    $text[$key] = str_replace("&", "cool", $value);
    $text[$key] = str_replace("’", "'", $value);
    $text[$key] = str_replace("“", "&quot;", $value);
    $text[$key] = str_replace("”", "&quot;", $value);
}



?>

Open in new window

LVL 4
Brant SnowAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

steveo225Commented:
Try:
<?php

$text = array(

        "Title" => "Hello & World!",
        "Heading" => "I’m",
        "Bottom" => "I hate & ””People"
)
?>


<?php


foreach($text as $key=>$value) {
    $value = str_replace("&", "cool", $value);
    $value = str_replace("’", "'", $value);
    $value = str_replace("“", "&quot;", $value);
    $text[$key] = str_replace("”", "&quot;", $value);
}



?>

Open in new window

0
rkeith2412Commented:
give this a try
$text = array( 
 
        "Title" => "Hello & World!", 
        "Heading" => "I’m", 
        "Bottom" => "I hate & ””People" 
); 

$find = array("&", "’", "“", "”");
$replace = array("cool", "'", "&quot;", "&quot;");

 
foreach($text as $key=>$value) { 
    $text[$key] = str_replace($find, $replace, $value); 
} 

Open in new window

0
john-formbyCommented:
Hi,

Please have a look at the following.

Hope this helps,

John
<?php 
$text = array( 
        "Title" => "Hello & World!", 
        "Heading" => "I’m", 
        "Bottom" => "I hate & ””People" 
) 
?> 

<?php 
foreach($text as $key=>$value) { 
    $text[$key] = str_replace(array("&","’","“","”"), array("cool","'","&quot;","&quot;"), $value); 
} 
print_r($text);
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.