How to Find and Replace Text in PHP


In this article, we will go over how you can find and replace text in PHP.

The function to find and replace text in PHP is the str_replace() function which accepts the following parameters in the format:

str_replace(text_to_replace, word/phrase_to_replace_text_with, which_block_of_text_this_affects);

This function will now be explained:

text_to_replace- The first parameter is the word/phrase which you want to replace. For example, if you want to replace the word 'John' in your paragraph with the word 'Kevin', John is the word that you want to replace. Therefore, it would be the first parameter of the str_replace() function.

word/phrase_to_replace_text_with- This is the parameter of the word you want to replace the text with. Let's use the same example as before. If you want to replace the word 'John' with 'Kevin', Kevin would be the 2nd parameter of this function, since you want 'Kevin' to replace 'John'.

which_block_of_text_this_affects- This is the of the block of code which you want the find/replace function to apply to. This can be a paragraph tag, a text box, a text area, any element on the web page. For example, if you want to carry out this function on a paragraph tag with a name attribute of paragraph1, then paragraph1 would be the 3rd parameter.


Now that we've gone over the str_replace() function, let's do an example to get a practical and real-world feel of it.

Below rare 2 text boxes and 1 text area. You can type your main text into the text area and can find and replace strings with the text boxes.



You can see above that the main text area to type your text into is the text area, which is a multi-line input area for typing text, such as a paragraph. The text boxes above this text area are the Find and Replace text boxes. These text boxes will find the text you want and replace it with the text you want, much like a standard Find & Replace feature that are common to many text editors or word programs, such as Microsoft Word and Notepad.


The HTML Code to create the above text boxes and text area is:

<form action="" method="post">
Find: <input type="text" name="find" value='<?php echo $find; ?>'/><br><br>
Replace: <input type="text" name="replace" value='<?php echo $replace; ?>'/><br><br>
<input type="submit" value="Replace"/><br><br>

<textarea name="maintext" rows="8" cols="80"><?php echo $newtext; ?></textarea>

Notice how all the form fields must have a name attribute. This is because we use this name attribute to allow PHP to extract data from each of the form fields, as you'll see below. PHP uses the name attribute to know which form field you are referring to.

PHP Code

The PHP code to create the find/replace function is:


$find= $_POST['find'];
$replace= $_POST['replace'];
$text= $_POST['maintext'];
$submitbutton= $_POST['submitbutton'];

if (isset($find) && isset($replace))
$newtext= str_replace($find, $replace, $text);


The PHP code extracts the information from each of the form fields. If it finds the word present which you want to replace in the text area of the main text, it will replace it with the text on the 'Replace' form field.

We extract the data from the form fields with the superglobal $_POST array. If the $find and $replace variables are set, then we carry out the str_replace function and assign it to the variable $newtext.

If you go back to the HTML code above, we echo out this $newtext in the text area, so that when the user presses the 'Replace' button, the new updated text will be displayed. This, again, is echoed between the <textarea></textarea> tags.

And this is how you can create a Find/Replace functionality on a web page.

Related Resources

How to Upload Images to a Website Using PHP

How to Upload Files to a Website Using PHP

How to Create Your Own File Transfer Protocol (FTP) For Your Website Using PHP

HTML Comment Box is loading comments...