Web Technologies
Slip22
Q.1) Design a HTML form to accept two strings from the user. Write a PHP function to find the first occurrence and the last occurrence of the small string in the large string. Also count the total number of occurrences of small string in the large string. Provide a text box to accept a string, which will replace the small string in the large string. (Use built-in functions) [Marks30]
Solution
a.php
<?php$large=$_POST['large'];
$small=$_POST['small'];
$replace=$_POST['replace'];
$op=$_POST['op'];
switch($op)
{
case 1: $pos=strpos($large,$small);
if($pos===false)
echo "$small is not present in $large";
else
echo "$small first occurence is at $pos";
break;
case 2: $pos=strrpos($large,$small);
if($pos===false)
echo "$small is not present in $large";
else
echo "$small last occurence is at $pos";
break;
case 3:
$n=substr_count($large,$small);
echo "$small is present $n times in $large";
break;
case 4:
$m=strlen($small);
while(($pos=strpos($large,$small))!==false)
{
$large=substr_replace($large,$replace,$pos,$m);
}
echo"Modified String is $large";
}
?>
b.html
<form method='post' action='a.php'><table>
<tr>
<td><b>Large String :</b></td>
<td><input type='text' name='large'></td>
</tr><tr>
<td><b>Small String :</b></td>
<td><input type='text' name='small'></td>
</tr><tr>
<td><b>Replacement String :</b></td>
<td><input type='text' name='replace'></td>
</tr>
</table>
<br>
<table>
<tr>
<td><input type='radio' name='op' value=1>First Occurence</td>
<td><input type='radio' name='op' value=2>Last Occurence</td>
<td><input type='radio' name='op' value=3>No.of Occurences</td>
<td><input type='radio' name='op' value=4>Replace</td>
</tr>
</table>
<br>
<table>
<tr>
<td> <input type='submit'></td>
<td> <input type='reset'></td>
</tr>
</table>
</form>
Tags:
Web Technologies