September 18, 2012 learn_php

Simple usage of str_replace in PHP

Simple usage 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.

Example would be:

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 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:

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.

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 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, with str_replace is enough to achieve our goal.

One thought on “Simple usage of str_replace in PHP

  1. people only looking for particular circumstances. The preg_match() features allows you to look for for and substitute within a sequence a non-fixed routine by using a frequent idea.

Leave a Reply

Your email address will not be published. Required fields are marked *

The qTranslate Editor has disabled itself because it hasn't been tested with your Wordpress version yet. This is done to prevent Wordpress from malfunctioning. You can reenable it by clicking here (may cause data loss! Use at own risk!). To remove this message permanently, please update qTranslate to the corresponding version.