Write a PHP script for find small string check occurrences and replace | PHP - IProgramX

Q. 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) 




HTML File:

<html>
<body>
<form action='ass1b2' method=post>
<pre>
Enter first string  :<input type='text' name='str1'><br>
Enter second string  :<input type='text' name='str2'><br>
Enter string for replace:<input type='text' name='str3'><br></h2>
                                                                                                                             
                        <input type='radio' name="ch" value=1>Occurence.
                        <input type='radio' name="ch" value=2>Replace.
                        <input type='radio' name="ch" value=3>split.                                                                                                   
                        <input type=submit value=ok>    <input type=reset value=cancel>
</pre>
</form>
</body>

</html>

PHP Function:
NOTE: PHP function is saved as "ass1b2.php"


<?php
        $str1=$_POST['str1'];
        $str2=$_POST['str2'];
        $str3=$_POST['str3'];
        $ch=$_POST['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>";
                }
        }

?>

Output:





Post a Comment

2 Comments

  1. Thank you for sharing this informative post. Looking forward to reading more.
    New Features and Updates in PHP 8

    ReplyDelete
  2. Thank you because you have been willing to share information with us. we will always appreciate all you have done here because I know you are very concerned with our. String functions php

    ReplyDelete