4]Write a PHP script for the following: Design a form to accept
two strings from the user. 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)
Slip4.html
<html>
<body>
<form action="slip4.php" method="get">
<center>
<table>
<tr><td>Enter First String : </td><td><input
type="text" name="str1"></td><tr>
<tr><td>Enter second String : </td><td><input
type="text" name="str2"></td></tr>
<tr><td>Enter String To Replace :
</td><td><input type="text"
name="str3"></td></tr>
<tr>
<td>Occurrences</td><td><input
type="radio" name="ch" value=1></td>
<td>Replace</td><td><input type="radio"
name="ch" value=2></td>
<td>Split</td><td><input type='radio'
name="ch" value=3></td>
</tr>
<tr><td></td><td><input
type="submit" value=Next></td></tr>
</table>
</center>
</form>
</body>
</html>
Slip4.php
<?php
$str1
= $_GET['str1'];
$str2
= $_GET['str2'];
$str3
= $_GET['str3'];
$ch
= $_GET['ch'];
echo"First
string=$str1.<br>";
echo"Second
String=$str2.<br>";
echo"String
for replace=$str3.<br>";
if(strlen($str1)>strlen($str2))
{
switch($ch)
{
case
1: $pos=strpos($str1,$str2);
if($pos!=0)
echo"String
'$str2' Not present at the start of
'$str1'.<br>";
else
echo"String
'$str2' Present at the strat of '$str1'.<br>";
break;
case
2:
$str4=str_replace($str2,$str3,$str1);
printf("\nAfter
replacing string::");
echo
$str4;
break;
case
3: $s=preg_split("//",$str1);
foreach($s
as $v) echo "\t$v <br>";
break;
}
}
else
{
switch($ch)
{
case
1:$pos=strpos($str2,$str1);
if($pos!=0)
echo"String
'$str1' Not present at the start of
'$str2'.<br>";
else
echo"String
'$str1' Present at the start of '$str2'.<br>";
break;
case
2: $str4=str_replace($str1,$str3,$str2);
echo
"After replacing string::$str4<br>";
break;
case
3:
echo"After
splitting the string::<br>";
$s=preg_split("//",$str2);
foreach($s
as $v) echo "\t$v <br>";
}
}
?>