2011-04-01 115 views
0

我想用$ sq中的单词加粗来回显$ desc。这是我的代码如下所示:这个PHP脚本有什么问题?

<?php 
$desc = "This a sentence witch contains 4 words on is Hello the other is moto the third is hoto and finally but not least nono."; 
$sq = "Hello moto hoto nono"; 
$pieces = explode(" ", $sq); 
foreach (array($pieces[0], $pieces[1],$pieces[2],$pieces[3],$pieces[4]) as $item) 
    $descr = str_replace($item, "<b>".$item."</b>", $desc); 
    echo $descr; 
?> 

感谢

+0

“这一句”忘了是 – user666605 2011-04-01 14:01:35

+0

对不起我的英语 – user666605 2011-04-01 14:14:43

回答

4

首先,你不必五行(0-4)在pieces阵列,但只有4(=> 0至3 )。 接下来,你不需要做这个:

foreach (array($pieces[0], $pieces[1],$pieces[2],$pieces[3],$pieces[4]) as $item) 

时,你可以做

foreach ($pieces as $item) 

有了改变,它应该工作的第一点,但你应该改变两者。

0

没有$件[4]。 0 =>你好; 3 => nono。

2

试试这个:

$desc = "This a sentence witch contains 4 words on is Hello the other is moto the third is hoto and finally but not least nono."; 
$sq = "Hello moto hoto nono"; 
$pieces = explode(" ", $sq); 
foreach ($pieces as $item) 
    $desc = str_replace($item, "<b>".$item."</b>", $desc); 
echo $desc; 

有两个错误(我认为):

  • 首先,循环指令是不正确的。
  • 回显的变量不正确(descrdesc)。

问候

+0

谢谢你这真的帮助! – user666605 2011-04-01 14:08:07