PHP Tutorial Example
Example
PHP Mail
PHP Libxml
PHP HTTP
PHP Filter
PHP Zip
PHP Directory
PHP Error
PHP SimpleXML
PHP Calendar
PHP Date
PHP Misc
PHP XML
PHP FTP
PHP Math
PHP MySQL
PHP Filesystem
PHP Array
PHP String
PHP Tutorial
PHP Introduction
PHP Installation
PHP Syntax
PHP Variables
PHP Echo
PHP Strings
PHP Operators
PHP Comments
PHP Include File
PHP Require
PHP If Statement
PHP If Else
PHP Elseif
PHP Switch
PHP Forms
PHP Functions
PHP Array
PHP While Loop
PHP For Loop
PHP For Each
PHP Do While
PHP POST and GET
PHP Magic Quotes
PHP htmlentities
PHP Files
PHP File
PHP File Create
PHP File Open
PHP File Close
PHP File Write
PHP File Read
PHP File Delete
PHP File Append
PHP File Truncate
PHP File Upload
PHP Strings
PHP strpos
PHP str replace
PHP substrreplace
PHP Capitalization
PHP explode
PHP implode
PHP Advanced
PHP Date
PHP Session
PHP Cookies
PHP Include
PHP Email
PHP Secure Email
PHP Error
PHP Exception
PHP Filter
PHP and AJAX
AJAX Intro
AJAX PHP
AJAX Database
AJAX XML
AJAX Live Search
AJAX RSS Reader
AJAX Poll
PHP XML
XML Expat Parser
XML DOM
XML SimpleXML
PHP Database
MySQL Introduction
MySQL Connect
MySQL Create
MySQL Insert
MySQL Select
MySQL Where
MySQL Order By
MySQL Update
MySQL Delete
PHP ODBC
PHP Misc
PHP connection aborted Function
Another key tool to have in your programming toolbox is the ability to quickly replace parts of a PHP string with new values. The str_replace function is similar to a word processor's "Replace All" str_replace function start mention replace with word and tell full string. command that lets you specify a word and what to replace it with, then replaces every occurrence of that word in the document.
str_replace has three parameters that are required for the function to work properly. str_replace(search, replace, originalString).
1. search - This is what you want to search your string for. This can be a string or an array. or any word
2. replace - All matches for search will be replaced with this value.which value user want to replace. This can be a string or an array.
3. originalString - This is what search and replace will be operating on. The str_replace function will return a modified version of originalString when it completes.
Imagine we are working at a school district and need to create a webpage for the students' parents. The webpage has an introduction string that we need to customize depending on if the student is male or female. With str_replace this is mighty easy.
//string that needs to be customized
$rawstring = "Welcome Birmingham parents. Your replaceme is a pleasure to have!";
//male string
$malestr = str_replace("replaceme", "son", $rawstring);
//female string
$femalestr = str_replace("replaceme", "daughter", $rawstring);
echo "Son: ". $malestr . "<br />";
echo "Daughter: ". $femalestr;
With these two gender customized strings created we could then provide a more engaging experience for the student's parents when they logged into the school website with their kid's credentials.
In the last example we only needed to replace one word replaceme in our string, but what if we wanted to replace many words? We could just use the function multiple times to get the job done, or we could create an array of placeholders and a second array of replace values to get it all done in one function call.
The key thing to understand with this technique is that you are creating two arrays that will be used to swap values. The first item in placeholders will be replaced by the first item in the replace values, the second item of placeholders replaced with the second in replace values and so on and so forth.
Let's extend our simple example to be a complete form letter addressed to a student's parents.
//string that needs to be customized
$rawstring = "Welcome Birmingham parent! <br />
Your offspring is a pleasure to have!
We believe pronoun is learning a lot.<br />
The faculty simple adores pronoun2 and you can often hear
them say \"Attah sex!\"<br />";
//placeholders array
$placeholders = array('offspring', 'pronoun', 'pronoun2', 'sex');
//male replace values array
$malevals = array('son', 'he', 'him', 'boy');
//female replace values array
$femalevals = array('daughter', 'she', 'her', 'girl');
//male string
$malestr = str_replace($placeholders, $malevals, $rawstring);
//female string
$femalestr = str_replace($placeholders, $femalevals, $rawstring);
echo "Son: ". $malestr . "<br />";
echo "Daughter: ". $femalestr;
Notice: there is a bug in this code. The placeholder pronoun2 did not get replaced in the way we intended (our strings have he2 and she2 instead of him and her). This is because all instances of pronoun were replaced first and the pronoun in pronoun2 was replaced at this time with he or she, making he2 or she2. When it was pronoun2's turn to be replaced, there were no matches to be found, so our string has no him or her.

