Problem:
You have a long string which contains the word “2012” several times. You want to replace the word by “2013”.
Solution:
Use str_replace() function.
Example:
<?php $search_for = "2012"; $replace_with = "2013"; $apply_to = "It is the last quarter of 2012. Lots of successful projects have been completed on 2012. Please add those on our portfolio and update the year at the bottom to 'Copyright 2012 ...All right reserved'."; echo str_replace($search_for,$replace_with,$apply_to); ?>
Output:
It is the last quarter of 2013. Lots of successful projects have been completed on 2013. Please add those on our portfolio and update the line at the bottom to ‘Copyright 2013 …All right reserved’.
How it works:
Line 2 | Variable $search_for contains value (2012) that we’ll use to search for. |
Line 3 | Variable $replace_with contains 2013 that replaces 2012. |
Line 4 | Variable $apply_to contains the string to be searched and replaced on. |
Line 5 | The function str_replace() finds the value(2012) of the first parameter($search_for) in the third parameter($apply_to) and replaces with the value(2013) of the second parameter($replace_with) |
str_replace() function at a glance
(PHP4, PHP5)
Usages:
str_replace() function finds all the occurrences of a string in a longer string and replaces those with another string and then the function returns it. The function may also returns an array which you’ll see shortly.
Syntax:
str_replace(search_for, replace_with, apply_to, no_of_replacement)
Parameters:
Parameter | What it does | |
search_for | Required | Specifies the value to search. This might also be an array. |
replace_with | Required | Specifies the value to replace in place of search_for. This might also be an array. |
apply_to | Required | Specifies the string to be searched and replaced on. This might also be an array. |
no_of_replacement | Optional | Specifies how many replacements occurred. |
Variations 1:
If the search_for is an array and replace_with is a string, then the replacement string(replace_with) will be used to replace each array element that occurs.
Example :
<?php $search_for = array("2010", "2011", "2012"); $replace_with = "2013"; $apply_to = "It is the last quarter of 2011. Lots of successful projects have been completed on 2012. The sales increase by 40% than the year 2010 and 2011 together. Please add those projects on our portfolio and update the year at the bottom to 'Copyright 2012 ...All right reserved'."; echo str_replace($search_for,$replace_with,$apply_to); ?>
Output:
It is the last quarter of 2013. Lots of successful projects have been completed on 2013. The sales increase by 40% than the year 2013 and 2013 together. Please add those projects on our portfolio and update the year at the bottom to ‘Copyright 2013 …All right reserved’.
Explanation:
Line 5 | As the search_for is an array containing values 2010, 2011, and 2012(line 2); and replace_with is a string with value 2013(line 3), so each occurrence of 2010, 2011, and 2012 finds in apply_to string is replaced by 2013. |
Variations 2:
If both search_for and replace_with are arrays with equal elements, then the function searches for each array element of search_for array and replace with the corresponding element of the replace_with array.
Example:
<?php $search_for = array("ASP", "SQL Server"); $replace_with = array("PHP", "MySQL"); $apply_to = "Use ASP and SQL Server to develop the project."; echo str_replace($search_for,$replace_with,$apply_to); ?>
Output:
Use PHP and MySQL to develop the project.
Explanation:
Line 5 | The function str_replace() replaces the first element of the $search_for array(ASP) finds in the string $apply_to by the first element of the $replace_with array(PHP). Same thing happens for the second element, so we find MySQL in place of SQL SERVER |
Variations 3:
If both search_for and replace_with are arrays and replace_with has fewer elements than search_for, then the empty value is used for the rest of the replace_with element to replace the corresponding value of the search_for.
Example:
<?php $search_for = array("ASP", "SQL Server", ”CSS”); $replace_with = array("PHP", ”MySQL”); $apply_to = "Use ASP and SQL Server to develop the project."; echo str_replace($search_for,$replace_with,$apply_to); ?>
Output:
Use PHP and MySQL to develop the project.
Explanation:
Line 5 | The function str_replace() replaces the first two elements of the $search_for array(ASP and SQL Server) finds in the string $apply_to by the first two elements of the $replace_with array(PHP and MySQL). As the replace_with array has one less element than the search_for array, an empty value is considered in its position and the function returns nothing in replacement of the CSS. |
Variations 4:
If apply_to is an arrays, then the search and replace is performed in each element of the array and returns the array.
Example:
<?php $search_for = array("ASP", "SQL Server"); $replace_with = array("PHP", "MySQL"); $apply_to = array("Use ASP and SQL Server to develop the project. ", "ASP and SQL Server are not free softwares."); $str = str_replace($search_for,$replace_with,$apply_to); foreach($str as $value) echo $value."<br />"; ?>
Output:
Use PHP and MySQL to develop the project.
Explanation:
Line 5 | As $array_to is an array (line 4), the replacements (PHP in place of ASP and MySQL in place of SQL Server) performs in both elements. |
Line 6 | To print the replaced array we used foreach loop. |