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 :
- First of all download and install xampp server in your machine .
- Let’s begin with creating a form .
- Go to Start->All Programs->XAMPP->XAMPP htdocs folder as shown below in the image ..
- In htdocs folder create a new folder and lets name the folder as search.
- Inside the search folder create a new file say index.php.
- Open the index.php file with any text editor for example Notepad++ and write the following code for the form creation:
- Here is the screen shot of the code :
- Now open up your browser and enter the following url http://localhost/Search/
- Thus you will have the following output :
- Now let’s write the logic for find and replace .
- Now in the same index.php file add the following code.
- Here is the screen shot for it :
- Now inside if statement take three variables which will store the values from the post variable and also perform validation checks on it.
- Here is the screen shot for it..
- 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 “.
- Now take two variable :
- Here is the screen shot of the code :
- Now let’s take one while loop inside the nested if statements so add the code as follows :
- Screen shot is shown below :
- Now add function to replace the string which is found
- Screen shot is given below :
- Now add the following line of code after the while loop to display the string in the replaced version.
- Here is the screen shot for it :
- Now here is the complete code for implementing find and replace functionality in php .
- Now open up the browser and enter the following url localhost/Search/index.php and you will have the following output :
- Now type the Following text in your text area as follows Android is used by Apple
- Corresponding to your search field write Apple
- Corresponding to your replace field write Google
- After filling up the respective fields you will have the following view of your window:
- So now when you click on the find and replace button you will see the following output :
- Thus ,we have successfully learnt implementing find and replace functionality in php.
<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']; //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'; }
//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);
//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);
//This will display the string which appears after replacement echo $text;
<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'; } } ?>
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!