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:
$text_string = 'This is a very long sentence.'
$search = 'long';
$replace = 'short';
$new = str_replace($search, $replace, $text_string);
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:
$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
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.