PHP脚本没有';不起作用.While Condition in a Loop


PHP Script doesn't work. While Condition inside a Loop

我希望这里有人能帮我。我只是编码了一点点,我被卡住了。给我一个提示会很好,这可以向我解释我错在哪里。

这项任务比我现在所做的要复杂一些。目前,我希望for循环检查字符串的每个子字符串。循环中有一个while条件,它应该不断地回显子字符串,直到子字符串变成一个空格。然后它应该停止回响。

我不明白为什么它不起作用。我的服务器出现超时。

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

这对于正则表达式来说要容易得多:

$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 );