PHP

 Home  Computers & Internet  Web Programming PHP
What is PHP?
Echo
Comments
Variables
Constants
Data Types
number_format()
Character Strings
Mathematical Operators
Comparison Operators
Logical Operators
Joining Strings
explode()
implode()
strtolower()
strtoupper()
strlen()
ucfirst()
ucwords()
strrev()
str_replace()
str_repeat()
trim()
strip_tags()
addslashes()
stripslashes()
strpos()
strrpos()
nl2br()
isset()
unset()
empty()
POST
GET
If Statements
If Else Statements
Elseif Statements
Switch Statements
For Loops
While Loops
Do While Loops
Foreach Loops
File Create
File Open
File Read
File Write
File Delete
fgets()
file_get_contents()
Date & Time
$_SERVER
Sessions
Cookies
Arrays

HOW TO USE THE str_replace() FUNCTION IN PHP

The str_replace() function is a handy little function that allows you to replace certain parts of a string with something else. It takes three parameters: what to replace, the new replacement and the actual string to perform these actions on.

Example 1:

$string="John is my name";
$newstring=str_replace("is", "was", $string);

echo $newstring;

The above will output: John was my name

Here we have took the string $string and replaced the word is with the word was.

Example 2:

Another way to do the same thing:

$find="mexico";
$replace_with="New York";

$string="once upon a time in mexico";

$newstring=str_replace($find, $replace_with, $string);

echo $newstring;

The above will output: once upon a time in New York

The word mexico is replaced with New York.

Note that you can also use this function on any single or multiple characters and not just words.

Home | Privacy Policy | Terms Of Use | Contact Us