Online Tutorials & Training Materials | STechies.com
Register Login
Advertisement

str_replace Function in PHP

|| 0

str_replace Function in PHP
Stechies

str_replace() is a PHP in built function. It is used to replace a string occurrence in the given string. It can be in the form of an array or string. Their are four parameters in which 3 are mandatory and one is optional

Syntax:
str_replace (search_agru, replace_argu, main_argu,  count)

search_argu

Required

String / Array

Value which need to search and replace.

replace_argu

Required

String / Array

Value which need to be replaced.

main_argu

Required

String / Array

Value on which search is done  

count

Optional

String

Number of occurrence of search.

str_replace is case-sensitive, it return string / array depending on the argument type we give.

Example 1: Replace sting with string

<?php
// input string 
$man_argu3 = "you may be the good Friend of maine. May god bless you."; 
// using str_replace() function 
$resStr = str_replace('you', 'him', $man_argu3); 
print_r($resStr); 
?>

Output

him may be the good Friend of maine. May god bless him. 

In this program show an example to search "you" in the string and replace with "him".

Example 2: Replace sting with string in array

<?php

$arry = array("black","Red","Purpul","yellow");
print_r(str_replace("Red","Blue",$arry,$i));
echo "<br>" , "   Total Changes done: $i";

?>

Output:

<Array
(
    [0] => black
    [1] => Blue
    [2] => Purpul
    [3] => yellow
)

Total Changes done: 1

In This Example, array we search the value red and then we replace the value Red with Blue

Example 3: Replace html in string

<?php
echo ("The original sentance is nThis is a test sentance replcement shown");
$src_rep = ("That is A test sentence for str_replace. nThat is A test sentence for str_replace. nThat is A test sentence for str_replace.");
echo str_replace("is","<snap>was </snap>",$src_rep,$count);
echo "nTotal number of replacements = " .$count;
?>

Output:

The original sentance is                                                                                                       
This is a test sentance replcement show                                                                                        
That <snap>was </snap> A test sentence for str_replace.                                                                        
That <snap>was </snap> A test sentence for str_replace.                                                                        
That <snap>was </snap> A test sentence for str_replace.                                                                        
Total number of replacements = 3  

In this program multiple changes are done uing str_replace()  part of string changed. The srting "this is a" chnages with "That was A" .

Example 4: Multiple replace string with array

<?php

// Multiple replacements, using just an array

$replacetxt = Array(
'old word' => 'new word',
'was' => 'has a',
'past' => 'future',
);
$originaltxt = "Every old word was a thing of the past looking for new...";
$replacedtxt = str_replace(array_keys($replacetxt), $replacetxt, $originaltxt);
echo $replacedtxt;
?> 

Output:

Every new word has a thing of the future looking for new...   

In this program peace of string in a sentance are changed using the str_replace() function. changes are old word' => 'new word',
'was' => 'has a',
'past' => 'future.

Program 5: Replace array with array

<?php 
// Input string 
$str = "You are eating fruits, vegetables, fiber every day."; 
// Array containing search string 
$searchVal = array("fiber", "vegetables", "fruits"); 
$replaceVal = array("macroni ", "ice cream", "sweet corn"); 
// Function to replace string 
$res = str_replace($searchVal, $replaceVal, $str); 
print_r($res);
?> 

Output:

You are eating sweet corn, ice cream, macroni  every day.

sub string is replace using the str_replace function.

Example 6: Replace hyphen with comma or replace characters in string

<?php
$string = "Nov 07- 08 and 14 - 15";
$string = str_replace("-",",",$string);
echo 
$string;
?>

Output

Nov 07, 08 and 14 - 15

using str_Replace function hyphen is changed with comma.


Advertisement
Advertisement
Advertisement