Implementing find and replace functionality in PHP

1
3812

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. Here is the screen shot of the code :
  11. screenshot of code for form development

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

  15. Now let’s write the logic for find and replace .
  16. Now in the same index.php file add the following code.
  17. Here is the screen shot for it :
  18. find and replace logic code

  19. Now inside if statement take three variables which will store the values from the post variable and also perform validation checks on it.
  20. Here is the screen shot for it..
  21. validation checks

  22. 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 “.
  23. php functionalities

  24. Now take two variable :
  25. Here is the screen shot of the code :
  26. find and replace in php

  27. Now let’s take one while loop inside the nested if statements so add the code as follows :
  28. Screen shot is shown below :
  29. php tutorial on find and replace

  30. Now add function to replace the string which is found
  31. Screen shot is given below :
  32. implement replace functionality using php

  33. Now add the following line of code after the while loop to display the string in the replaced version.
  34. Here is the screen shot for it :
  35. display text in php

  36. Now here is the complete code for implementing find and replace functionality in php .
  37. Now open up the browser and enter the following url localhost/Search/index.php and you will have the following output :
  38. form output

  39. Now type the Following text in your text area as follows Android is used by Apple
  40. Corresponding to your search field write Apple
  41. Corresponding to your replace field write Google
  42. After filling up the respective fields you will have the following view of your window:
  43. filling of text fields

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

  46. 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