1. Home
  2. Computing & Technology
  3. PHP / MySQL

Str_replace () PHP Function

By Angela Bradley, About.com

Definition: You can use str_replace in PHP to find and replace data with alternate data. For example you could use it to filter forum posts to find offensive words and replace them with asterisks. It is phrased as: str_replace ( search_for, replace_with, apply_to )

The first parameter is what you are searching for, the second parameter is what you will replace it with, and the third parameter is the string you want searched. All three parameters can use an array as input if desired.

Also Known As: String Replace
Examples:
$cat = str_replace("kitty", "puppy", "I love my kitty");
Print $cat;
This would output: I love my puppy
$numbers = array("1", "2", "3");
$words = array("one", "two", "three");
$phrase = "I have 1 daughter and 3 sons";
$change = str_replace($numbers, $words, $phrase);
Print $change;
This would output: I have one daughter and three sons
$vowels = array("a", "e", "i", "o", "u");
$no = str_replace($vowels, "", "My name is Joe Smith");
Print $no;
This would output: My nm s J Smth

Explore PHP / MySQL

More from About.com

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. PHP Functions
  5. Str_replace - String Replace - Replace PHP

©2008 About.com, a part of The New York Times Company.

All rights reserved.