Answer:
<?php
$error = '';
$string = "This is a sample string. Hello World";
if(isset($_POST["modify_str"])){
$show_form = false;
extract($_POST);
// check that user entered some value
if(empty($string_search)){
$error += 'Please fill out the string to be searched for <br>';
$show_form = true;
}
// check that user entered some value
if(empty($string_replace)){
$error += "Please fill out the string/word to be replaced for <br>";
$show_form = true;
}
// if user has entered some value, then replace it in the string using the str_replace function and print the string after modification
if (!$show_form){
$string = str_replace($string_search, $string_replace, $string);
echo "String after modification: <br>";
echo $string;
}
}
else
{
$show_form = true;
}
if($show_form)
{
?>
Please select any word to search for and replace for in the following string <br>
<?php echo $string; ?>
<br> <br>
<form action="" method="post">
<label for="string_search"> String to search for: </label>
<input type="text" name="string_search" id="string_search" required="required" /> <br> <br>
<label for="string_replace"> Replace with: </label>
<input type="text" name="string_replace" id="string_replace" required="required" /> <br> <br>
<input type="submit" id="modify_str" name="modify_str" value="Modify String"/>
</form>
<?php
}
?>
Step-by-step explanation:
The program was written with php.
It is designed such that it can create 2 text field web form where 1 field allows the user to specify the text spring to search for and the other field allows the user to specify the replacement string.
See answer for the program code.