<?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>
If the word to be replaced is the first word or the only word in the string then it doesn't work but if the word to be replaced is in any other position except the first then it works fine.