<?php
$offset =0;
if (isset ($_POST['text']) && isset($_POST['searchfor'])&&isset($_POST['replacewith']))
{
$text = $_POST['text'];
$search = $_POST['searchfor'];
$replace = $_POST['replacewith'];
$length = strlen($search);
if (!empty($_POST['text'])&& !empty($_POST['searchfor'])&&!empty($_POST['replacewith']))
{
while ($stringpos = strpos($text, $search, $offset))
{
$offset = $stringpos + $length;
$text = substr_replace($text, $replace, $stringpos, $length);
}
echo $text;
}
else
{
echo 'Please fill in all the fields';
}
}
?>
<form action=53fineandreplace.php method="POST">
<textarea name = "text" rows="6" cols = "30"></textarea><br><br>
Search For:<br>
<input type= "text" name = "searchfor"><br><br>
Replace with:<br>
<input type="text" name = "replacewith"><br><br>
<input type = "submit" value = "Submit">
</form>
如果要替换的单词的第一个字或词只在字符串中那么它不工作,但如果要替换的单词在除了第一个之外的任何其他位置,那么它工作正常。为什么这个PHP substr_replace()不替换第一个字