1. Computing & Technology

Discuss in my forum

Str_replace () PHP Function

By , About.com Guide

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

©2012 About.com. All rights reserved.

A part of The New York Times Company.