Click here to Skip to main content
13,346,966 members (64,403 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked
Posted 8 Oct 2012

Simple Usage of str_replace in PHP

, 8 Oct 2012
Rate this:
Please Sign up or sign in to vote.
Uusage of str_replace in PHP

While working with PHP, I have found myself with the need of changing at least one line of code within a PHP file. This line could be within a config.php file, in which we are telling the site the exact settings that are needed. An example of this could be that you are creating a small CMS, and are distributing it to a few people or so. That CMS you are building will dynamically configure itself to the current environment, and depending on user inputs. I had a similar project, in which the websites were being created automatically, and I took a config file, and replaced something like, ‘$sitename = “”;’ within the file, and changed it with the actual sitename and domain name. This was done with a very simple PHP function call: str_replace (string replace).


The syntax for this function is very simple. When calling str_replace, you would pass on the search term (needle in the haystack), replacement text, and the string to be evaluated.

An example would be:

    // This is an example usage
    $text_string = 'This is a very long sentence.'
    $search = 'long';
    $replace = 'short';
    $new = str_replace($search, $replace, $text_string);
    echo $new;

The above example would print:

This is a very short sentence.

As you can see, str_replace will look for a specific string and replace it with what you want.

Some Common Uses

I'm sure most of you have been to a forum, and have posted at least one comment. If you see, most forums accept for users to type in smileys. With str_replace, this is possible. All we have to add is an array of strings, and then add another array of replacement images. Let’s do that right now:

    // Grab initial input
    $text = $_POST['comment'];
    $text_smiles = array(":)", ":D", ":(", ":'(", ":@", ":P" );
    $image_smiles = array("<img src='smile.gif'>", "<img src='laugh.gif'>", 
      "<img src='sad.gif'>", <img src='cry.gif'>", 
      "<img src='mad.gif'>", "<img src='tounge.gif'>");
    // Replace input with smileys if present
    $content = str_replace($text_smiles, $image_smiles, $text);    

When user submits their comment, it should go through this mini process, to enable the smileys to display when the comment is being displayed.

Notice that the order is very important, as str_replace will replace a string in the same position in which the replace value is in, within the array. If the replace array has fewer values than the search array, then the text beyond the text values will be replaced with empty strings.

Another usage might be with specific bad words. In this case, we can use an array for all the known bad words, and replace them with asterisks.

    $bad_words = ('duck', 'goose', 'beach', 'mother trucker', 'and many more');
    $replace = '***';
    $text = $_POST['input'];
    $filtered = str_replace($bad_words, $replace, $text);

That should allow you to filter these unwanted words from within your website.


Some people might ask, why not use preg_match() instead of str_replace(). The answer is simple, using str_replace is a lot faster than preg_match(), and we are only looking for specific words. The preg_match() functions allows you to search for and replace within a string a non-fixed pattern by using a regular expression. The use of regular expressions will however probably be more efficient in cases where you would have to use multiple calls to str_replace() to accomplish the same thing. For now, str_replace is enough to achieve our goal.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
Puerto Rico Puerto Rico
As a computer scientist, I strive to learn more everyday in my field. Everything I learn, I share with my community by writing articles for future references.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 4 Pin
Wen Hao9-Oct-12 18:03
memberWen Hao9-Oct-12 18:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 8 Oct 2012
Article Copyright 2012 by jotorres
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid