2014-02-22 33 views
0

我想在foreach使用str_replace函数,但它不工作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 $键=> $ value`

但遗憾的是它不工作

我怎样才能解决这个问题?

+0

无需使用的foreach:只使用'$ newhtml = str_replace函数(array_keys($ random_name),$ random_name,$ HTML);' –

+0

@MarkBaker由于某种原因,我必须使用'foreach'为什么它不适用于foreach? – user3325376

+0

这是一个可恶的数组密钥滥用。另外,它可能有助于说明它不起作用。预期与实际产出。 – miken32

回答

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; 
+0

谢谢,它的工作很好,但你能解释为什么它不能在'$ html'中工作,我们只是''html'复制到'$ test3'为什么我们不能直接使用它? – user3325376

+0

你也可以直接使用,我刚刚给你提供了另一种方法,如果你可能需要原始的$ html的地方:)但如果你不需要真正的,那么最好使用直接的方式。 – iCore

+0

非常感谢:),现在我明白了,但为什么我不能使用像'$ test3 = str_replace($ key,$ value,$ html)的其他变量名称;'这是什么问题? – user3325376

0

你总是用不变的变量$html作为变量做的改变,所以只有最后一次迭代你的foreach循环将反映在您的最终值$test3

0

一号线的解决方案:

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