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)
a1setb2.html
<html>
<form action='setb2.php' method='post'>
Enter 1'st string :<input type='text' name='str1'><br>
Enter 2'nd string :<input type='text' name='str2'><br>
Enter string to replace small string :<input type='text' name='str3'><br>
<input type='radio' name='ch' value=1>Occurence at start of the large string.<br>
<input type='radio' name='ch' value=2>Replace<br>
<input type='radio' name='ch' value=3>Split<br>
<input type=submit value= submit>
<input type=reset value=cancel>
</form>
</body>
</html>
setb2.php
<?php
$str1 = $_POST['str1'];
$str2 = $_POST['str2'];
$str3 = $_POST['str3'];
$ch = $_POST['ch'];
echo "First String is = $str1<br><br>";
echo "Second String is = $str2<br><br>";
echo "String for Replace is = $str3<br><br>";
switch($ch)
{
case 1 :
if(!empty($str1)&&!empty($str2))
{
$p = preg_match("/^$str2/","$str1");
echo $p;
if($p==1)
echo "String is present at the start of '$str1'.<br>";
else
echo "String is not present at the start of '$str1'.<br>";
}
else
echo "Strings are empty.";
break;
case 2 :
if(!empty($str1)&&!empty($str2)&&!empty($str3))
{
$str4 = preg_replace("/$str2/","$str3","$str1");
echo "After replacing string $str4";
}
else
echo "Strings are empty.";
break;
case 3:
if(!empty($str1))
{
$split=preg_split("/ /","$str1");
foreach($split as $value)
{
echo "<br>$value.";
}
}
else
echo "Strings are empty..";
break;
}
?>
0 Comments
If anyone has Doubts or suggestions please let me know