Space for google add

Find the first occurrence and the last occurrence of the small string in the large string in PHP .

 

Write a PHP script for the following: Design a form to accept two strings from
the user. 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)




a1seta2.html

<html>
<form action='seta2.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='str'><br>                                                                                            
        <input type='radio' name='ch' value=1>First and Last occurrence<br>
        <input type='radio' name='ch' value=2>Total Occurrence<br>
        <input type='radio' name='ch' value=3>replace<br>
        <input type='Submit'  value= 'Submit' >
       <input type=reset value=cancel>
</form>
</body>
</html>





seta2.php

<?php
        $str1=$_POST['str1'];
        $str2=$_POST['str2'];
        $str=$_POST['str'];
        $ch=$_POST['ch'];
        $cnt=0;
        echo"<br>string1::$str1<br>string2:: $str2<br>string for replace:: $str</b><br>";
        switch($ch)
                {
                case 1:
if(!empty($str1) && !empty($str2))
{
echo"<br>First occurrence  is at possition :";
echo strpos($str1,$str2);
                            echo"<br>Last  occurrence is at possition :";
echo strrpos($str1,$str2);
}
else
echo "Strings are empty..";
                        break;
                case 2:
if(!empty($str1) && !empty($str2))
{
$cnt=substr_count($str1,$str2);
                        if($cnt==0)
                                echo"<br>string '$str2' not present in string '$str1'<br>";
                        else
                                 echo"<br>sub string '$str2' present $cnt times in string '$str1'<br>";
}
else
echo "Strings are empty..";
                        break;
                case 3:
if(!empty($str1) && !empty($str2)&&!empty($str))
{
                                $str3=str_replace($str2,$str,$str1);
                                echo"<br>After relacing string is::$str3";
}
echo "<br><br>Strings are empty.";
                        break;
                }  
?>

Post a Comment

0 Comments