Question

i want to use str_replace in foreach but it's not working

<?php
$html = '<p><span style="color: #0099cc;">This is Some Text</span><span style="color: #0099cc;"><br /><br />This is Some Text<br /></span><br /><span style="color: #009999;">This is Some Text<br /><br /></span>This is Some Text<br /><br /><span style="color: #336699;">This is Some Text</span><br /><span style="color: #ff0066;">This is Some Text<br /><br />This is Some Text<br /></span><br /><span style="color: #ff0066;">This is Some Text</span><br />This is Some Text<br /><br /><img src="http://shop.about.com/pic/android2014-1.jpg" alt="" /></p>
<p style="text-align: center;"><strong><span style="color: #ff0000;">Price 11111</span></strong></p>
<p><a href="addcart.php?action=add&amp;id=373" target="_blank"><img src="http://shop.about.com/pic/buy2.gif" alt="" border="0" /></a><br /><br /></p>
<div align="justify">This is Some Text<br />This is Some Text<br /><br />This is Some Text<br />This is Some TextThis is Some Text<br /><br />
<div align="center"><img src="http://shop.about.com/pic/android2014-2.jpg" alt="" /><br />
<p style="text-align: center;"><strong><span style="color: #ff0000;">This is Some Text:</span>This is Some Text</strong></p>
<a href="addcart.php?action=add&amp;id=373" target="_blank"><img src="http://shop.about.com/pic/buy2.gif" alt="" border="0" /></a><br /><br />
<div align="justify">This is Some Text<br />This is Some Text<br />This is Some Text<br />This is Some Text<br />This is Some Text<br />This is Some Text<br /><br />
<div align="center"><img src="http://shop.about.com/pic/android2014-3.jpg" alt="" /><br /><br />
<div align="justify">This is Some Text<br /><br />
<div align="center"><strong><span style="color: #ff0000;">This is Some TextThis is Some Text</span>This is Some Text) This is Some Text</strong><br /><br /><a href="pic/android2014-7.jpg" target="_blank"><img src="http://shop.about.com/pic/android2014-6.jpg" alt="" align="bottom" border="0" hspace="0" vspace="0" /></a> <a href="pic/android2014-5.jpg" target="_blank"><img src="http://shop.about.com/pic/android2014-4.jpg" alt="" /></a><br />
<div align="justify">
<div align="justify">
<p style="text-align: justify;"><span style="font-weight: normal;" lang="fa"><span style="color: #ff0000;">This is Some Text:</span>This is Some Text</span></p>
</div>
</div>
<br />
<p style="text-align: center;"><br /><strong><span style="color: #ff0000;">This is Some Text: </span>This is Some This is Some Text</strong></p>
<a href="addcart.php?action=add&amp;id=373" target="_blank"><img src="http://shop.about.com/pic/buy2.gif" alt="" border="0" /></a></div>
</div>
</div>
</div>
</div>
</div>';



$random_name = array(
"http://shop.about.com/pic/android2014-1.jpg" => "127.0.01/857513428.jpg" ,
"http://shop.about.com/pic/buy2.gif" => "127.0.01/673828126.jpg" ,
"http://shop.about.com/pic/android2014-2.jpg" => "127.0.01/824005127.jpg" ,
"http://shop.about.com/pic/android2014-3.jpg" => "127.0.01/927673340.jpg" ,
"http://shop.about.com/pic/android2014-6.jpg" => "127.0.01/274383545.jpg" ,
"http://shop.about.com/pic/android2014-4.jpg" => "127.0.01/175170899.jpg"


);

foreach ( $random_name as $key => $value ) {
$test3 = str_replace($key , $value , $html);    

}


echo $test3;

?>

so here you can see

i have one variable with name $html and one array with name $random_name

i want to replace all values in $random_names with them keys into $html

so i write foreach' as$key => $value`

but unfortunately it doesn't working

how can i solve this problem ?

Was it helpful?

Solution

If you want to replace directly, then instead of this:

$test3 = str_replace($key , $value , $html);

write this:

$html = str_replace($key , $value , $html);

and at the end, echo $html.

Or if you want to store it in a $test3 variable, then something like this:

$test3 = $html;
foreach ( $random_name as $key => $value ) {
    $test3 = str_replace($key, $value, $test3);
}
echo $test3;

OTHER TIPS

You are always using the unchanged variable $html as the variable to do the change in, so only the last iteration of your foreach loop will be reflected in your final value of $test3.

One line solution:

$replaced_urls = str_replace(array_keys($random_name), array_values($random_name), $html);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top