Web Technologies
Slip24
Q.1) Design a HTML form to accept two strings from the user. Write a PHP function to find whether the small string appears at the start of the large string. Provide a text box to accept the string that will replace all occurrences of small string present in the large string. Also split the large string into separate words. (Use regular expressions) [Marks30]
Solution
a.php
<?php$large=$_POST['large'];
$small=$_POST['small'];
$replace=$_POST['replace'];
$op=$_POST['op'];
switch($op)
{
case 1:
if(ereg("^$small",$large))
echo "$large starts with $small";
else
echo "$large doesn't starts with $small";
break;
case 2:
$replace_str=ereg_replace($small,$replace,$large);
echo "Modified String :$replace_str";
break;
case 3:
$words=split(' ',$large);
foreach($words as $k=>$v)
echo "$k:$v<br>";
}
?>
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>
Select Option :<br>
<table>
<tr>
<td><input type='radio' name='op' value=1>Start with</td>
<td><input type='radio' name='op' value=2>Replace</td>
<td><input type='radio' name='op' value=3>Words</td>
</tr>
</table>
<br>
<table>
<tr>
<td> <input type='submit'></td>
<td> <input type='reset'></td>
</tr>
</table>
</form>
Tags:
Web Technologies