PHP str_replace and str_ireplace

str_replace is a php function that allow programmer to quickly replace parts of a PHP string with new values, this is used for example to replace all words in some article to a new words. So if you want to replace all words "Jack" to "Smith", this is very easy in php just with using str_replace.

In this example we will try to replace "Today" to "Tomorrow" in the string: "Today is the first day of the month".

So, to do it step by step, we need to tell php what text it will search for, and with what text it will be replaced, and in what sring it will search.

PHP script:
<?php

$newstring = str_replace('Today','Tomorrow','Today is the first day of the month') ;
echo $newstring ;

?>
Display:
Tomorrow is the first day of the month
If there are many "Today" in the string, str_replace will replace all them to "Tomorrow"
<?php

$newstring = str_replace('Today','Tomorrow','Today is the first day of the month, and Today is 9/1/2008') ;
echo $newstring ;

?>
Display:
Tomorrow is the first day of the month, and Tomorrow is 9/1/2008
NOTE: This function is case-sensitive, just use str_ireplace() for case-insensitive replace:
<?php

$newstring1 = str_replace('Today','Tomorrow','today is 9/1/2008') ;
echo $newstring1 ;
echo '<br />' ;
$newstring1 = str_ireplace('Today','Tomorrow','today is 9/1/2008') ;
echo $newstring1 ;

?>
Display:
today is 9/1/2008 // the str_replace doesn't work, "today" wasn't replaced
Tomorrow is 9/1/2008 // the str_ireplace works well, "today" was replaced
Multiple replaces:
But what if you need to do many replaces for different searches ? In php you can do mutiple replaces in one, let's say for example in the string: My name is Jack and I'm 28. You would like to replace Jack to Smith and 28 to 43, so here you can use two str_replace function, one to replace Jack to Smith and one to replace 28 to 43.
But a beautiful tool in php is that you can do that in one replace function. You need to create two array: an array for the searches and an array for the replaces:

PHP Code:
<?php

$newstring1 = str_replace('Jack','Smith','My name is Jack and I\'m 28') ;
$newstring2 = str_replace('28','43', $newstring1 ) ;
echo $newstring2 ; // My name is Smith and I'm 43

echo '<br />' ;

//The new tool:
$searches = array('Jack' , '28' );
$replaces = array('Smith' , '43' );
$newstring = str_replace( $searches , $replaces ,'My name is Jack and I\'m 28') ;
echo $newstring ;

?>
Display:
My name is Smith and I'm 43 // the old methode: multiple replaces.
My name is Smith and I'm 43 // the new tool: all replaces in one.
Do you see the backslashes in "am I\'m 28" , this is because if we don't add it, the quote of I'm will be considered as the end of the string: 'My name is Jack and I' .

You can also in php do the same multiple replaces to replace different words to one word, it's the same as the last script, but we will not need aa array for replaces, but only an array for searches.

For example if you have a string: "I need 3 books, 2 copybooks and 4 pens", and you need to replace all "3" and "2" and "4" to 5 , you will do it just like the following:

PHP Code:
<?php

$searches = array('3' , '2' , '4');
$newstring = str_replace( $searches , ' 5 ' ,'I need 3 books, 2 copybooks and 4 pens') ;
echo $newstring ;

?>
Display:
I need 5 books, 5 copybooks and 5 pens

9 comments:

Julian_Belfort said...

Nice! thank you very much!

John Mueller said...

Nice blog, functions are very well explained, thank you !

ILyes said...

you are welcome :)

Cegonsoft said...

It's really a nice content,
and the explantion is perfect.

Guild Wars 2 Gold said...

it is really my personal hornor to possess a look at your website,it is great. Buy RS Gold



GW2 Gold

PHP Tutorial For Beginners With Examples PDF said...

great site. Will recommend to my facebook colleagues.

kimjhon said...

Learned a lot of new things from your post! Good creation and HATS OFF to the creativity of your mind. Very interesting and useful blog!
PHP Training in Chennai
PHP Training Chennai

Blogger said...

Hey, Wow all the posts are very informative for the people who visit this site. Good work! We also have a Blog.Please feel free to visit our site. Thank you for sharing.
php training center in indore
Keep Posting:)

Shivam Bansal said...

Thanks for such information" Braiding Shop"