PHP脚本不起作用。而环境中的条件

时间:2013-11-27 22:02:26

标签: php html

我希望有人能在这里帮助我。我只是编码了一点而且我被卡住了。提示会很棒,这向我解释,我错了。

这项任务比我现在所做的更复杂。目前,我希望for循环检查字符串的每个子字符串。循环内部是一个while条件,它应该不断回显子串,直到子串是一个空格。然后它应该停止回声。

我不明白为什么它不起作用。我的服务器上有一个TimeOut。

<?php
// Task
// Find-and-Replace
// "whole word" match
// Text is sorted as array of character

$text = "This film is the best film of the year."; // Find: film, replace: movie
$word = "film";
$replace = "movie";
echo strlen($text) . "<br>";

for($i = 0; $i <= strlen($text); $i++){
    while(substr($text,$i,1) != " ") {
        echo substr($text,0,1);
    }
}



?>

编辑: 它仍然不起作用:

for($i = 0; $i <= strlen($text); $i++){
    while(substr($text,$i,1) != " ") {
        echo substr($text,$i,1);
    }
}

没有While循环,它会遍历每个字母。但它不适用于whileloop。

1 个答案:

答案 0 :(得分:0)

使用正则表达式会更容易:

$text = "This film is the best film of the year."; // Find: film, replace: movie
$word = "/film/i";
$replace = "movie";

echo preg_replace($word, $replace , $text );