195. PHP Program to replace a particular word in a given string and print modified string.
How does this program work?
- This program is used to replace a particular word in a given string and print modified string using PHP.
Here is the code
<html>
<head>
<title>PHP Program To replace a particular word in a given string </title>
</head>
<body>
<form method="post">
<table border="0">
<tr>
<td> <input type="text" name="input" value="" placeholder="Enter string"/> </td>
</tr>
<tr>
<td> <input type="text" name="input1" value="" placeholder="Enter modified word"/> </td>
</tr>
<tr>
<td> <input type="text" name="input2" value="" placeholder="Enter replace word"/> </td>
</tr>
<tr>
<td> <input type="submit" name="submit" value="Submit"/> </td>
</tr>
</table>
</form>
<?php
if(isset($_POST['submit']))
{
$str = $_POST['input'];
$mod = $_POST['input1'];
$rep = $_POST['input2'];
echo "Given String : ".$str."</br>";
echo "Modified word : ".$mod."</br>";
echo "Replace word : ".$rep."</br>";
//To modify the given string
$sen = str_replace("$mod", "$rep", $str);
echo "The modifed String is: ".$sen;
}
?>
</body>
</html>