str_replace不适用于每个

时间:2014-02-22 22:39:54

标签: php str-replace

我想在foreach中使用str_replace,但它不能正常工作

<?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;

?>

所以在这里你可以看到

我有一个名为$html的变量 和一个名为$random_name

的数组

我想将$random_names中的所有值替换为$html

所以我写foreach' as $ key =&gt; $ value`

但遗憾的是它不起作用

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:1)

如果你想直接替换,那么代替:

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

写下这个:

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

最后,echo $html

或者如果你想将它存储在$test3变量中,那么就像这样:

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

答案 1 :(得分:0)

您始终使用未更改的变量$html作为变量进行更改,因此只有foreach循环的最后一次迭代才会反映在$test3的最终值中

答案 2 :(得分:0)

一线解决方案:

$replaced_urls = str_replace(array_keys($random_name), array_values($random_name), $html);