Implementing find and replace functionality in PHP

1
4958

In this session today we will be implementing find and replace functionality in php.Here we will be creating a form which will have an text area , search box , replace box and an find and replace button .

  • For implementing find and replace functionality in php follow the steps given below :

  1. First of all download and install xampp server in your machine .
  2. Let’s begin with creating a form .
  3. Go to Start->All Programs->XAMPP->XAMPP htdocs folder as shown below in the image ..
  4. implementing find and replace functionality in php

  5. In htdocs folder create a new folder and lets name the folder as search.
  6. find and replace functionality

  7. Inside the search folder create a new file say index.php.
  8. find and replace function in php

  9. Open the index.php file with any text editor for example Notepad++ and write the following code for the form creation:
  10. <form action="index.php" method="POST">
        <label>Type your Text Here</label><br><br>
        <textarea name="textarea" cols="40" rows="10"></textarea><br><br>
        Search For Text<br>
        <input type="text" name="search"><br><br>
        Replace Text With<br>
        <input type="text" name="replace"><br><br>
        <input type="submit" value="Find and Replace">
    </form>
    

  11. Here is the screen shot of the code :
  12. screenshot of code for form development

  13. Now open up your browser and enter the following url http://localhost/Search/
  14. Thus you will have the following output :
  15. form for finding and replacing text

  16. Now let’s write the logic for find and replace .
  17. Now in the same index.php file add the following code.
  18. <?php
    //This will will take all the values from textarea and text from the html form
    if(isset($_POST['textarea']) && isset($_POST['search']) && isset($_POST['replace']))
    {
        
    }
    ?>
    

  19. Here is the screen shot for it :
  20. find and replace logic code

  21. Now inside if statement take three variables which will store the values from the post variable and also perform validation checks on it.
  22. //stores the value from the post variables
         $text = $_POST['textarea'];
         $search = $_POST['search'];
        $replace = $_POST['replace'];
        
        //validation is made to check if all the fields are entered
        if(!empty($text) && !empty($search) && !empty($replace)){
        
        }else{
            echo 'Please fill all the fields';
        }
    

  23. Here is the screen shot for it..
  24. validation checks

  25. So now if you run your programme and forget to fill any of the field it will pop-up an message which will say ” Please fill all the fields “.
  26. php functionalities

  27. Now take two variable :
  28.     //This is to point to the string characters to search for the matching pair
        $offset = NULL;
        //This variable is used to store the length of the search string 
        $searchLength = strlen($search);
    

  29. Here is the screen shot of the code :
  30. find and replace in php

  31. Now let’s take one while loop inside the nested if statements so add the code as follows :
  32. //The while loop takes three parameters 
    //first parameter will take the current text 
    //second parameter will take the text which wants to search
    //third parameter will take the character where the search is matched
    while ($stringPostion = strpos($text, $search, $offset))
                    {
                        //This will check if we have visited all the characters of the string
                        $offset = $stringPostion + $searchLength;
                        
                    }
    

  33. Screen shot is shown below :
  34. php tutorial on find and replace

  35. Now add function to replace the string which is found
  36.       //This function takes four parameters 
                        //First param checks the string to check
                        //second param replaces the find string
                        //third param specify from where the string replacing should start
                        //fourth param specify how many character should be replaced
    
                        $text = substr_replace($text, $replace, $stringPostion, $searchLength);
    

  37. Screen shot is given below :
  38. implement replace functionality using php

  39. Now add the following line of code after the while loop to display the string in the replaced version.
  40. //This will display the string which appears after replacement
    echo $text;
    

  41. Here is the screen shot for it :
  42. display text in php

  43. Now here is the complete code for implementing find and replace functionality in php .
  44. <form action="index.php" method="POST">
        <label>Type your Text Here</label><br><br>
        <textarea name="textarea" cols="40" rows="10"></textarea><br><br>
        Search For Text<br>
        <input type="text" name="search"><br><br>
        Replace Text With<br>
        <input type="text" name="replace"><br><br>
        <input type="submit" value="Find and Replace">
    </form>
    
    <?php
    //This will will take all the values from textarea and text from the html form
    if(isset($_POST['textarea']) && isset($_POST['search']) && isset($_POST['replace']))
    {
        //stores the value from the post variables
         $text = $_POST['textarea'];
         $search = $_POST['search'];
        $replace = $_POST['replace'];
        //This is to point to the string characters to search for the matching pair
        $offset = NULL;
        //This variable is used to store the length of the search string 
        $searchLength = strlen($search);
        
        //validation is made to check if all the fields are entered
        if(!empty($text) && !empty($search) && !empty($replace)){
            
            //The while loop takes three parameters 
            //first parameter will take the current text 
            //second parameter will take the text which wants to search
            //third parameter will take the character where the search is matched
            while ($stringPostion = strpos($text, $search, $offset))
                    {
                        //This will check if we have visited all the characters of the string
                        $offset = $stringPostion + $searchLength;
                        //This function takes four parameters 
                        //First param checks the string to check
                        //second param replaces the find string
                        //third param specify from where the string replacing should start
                        //fourth param specify how many character should be replaced
                        $text = substr_replace($text, $replace, $stringPostion, $searchLength);
                    }
                    echo $text;
        }else{
            echo 'Please fill all the field';
        }
    }
    
    ?>
    

  45. Now open up the browser and enter the following url localhost/Search/index.php and you will have the following output :
  46. form output

  47. Now type the Following text in your text area as follows Android is used by Apple
  48. Corresponding to your search field write Apple
  49. Corresponding to your replace field write Google
  50. After filling up the respective fields you will have the following view of your window:
  51. filling of text fields

  52. So now when you click on the find and replace button you will see the following output :
  53. find and replace in php

  54. Thus ,we have successfully learnt implementing find and replace functionality in php.

1 COMMENT

  1. Hey man. Great tutorial, but i seem to be stuck on something. How would you go about finding and replacing several strings in the text? I really appreciate your help buddy!

LEAVE A REPLY

Please enter your comment!
Please enter your name here