String Replacement

substr_replace($s1,$s2,$i1,[$i2]) returns a copy of $s1, with the portion of length $i2 starting from $i1 replaced by $s2. If $s1 is an array of strings, the replacement will occur on each srting, in which case $s2, $i1 and $i2 can be scalar values or arrays. If $i1 is negative, the replacement will begin at the $i1-th character from the end. If $i2 is negative, it represents the number of characters from the end of $s1 at which to stop replacing. If $i2 is omitted, it will default to strlen($s1). str_replace($s1,$s2,$s3[,&$i]) returns a copy of $s3 with all $s1 replaced by $s2. $i stores the number of replacements performed. If $s3 is an array, the search and replace is performed with every entry, and the return value is an array. If $s1 and $s2 are arrays, then a value from each array will be taken to search and replace on $s3. str_ireplace(……) is the case-insensitive version of str_replace(……). strtr($s1,$s2,$s3) gives a copy of $s1 where all occurrences of each character in $s2 have been translated to the corresponding character in $s3. strtr($s1,$arr) does almost the same thing, but the second argument is an array in the form (‘from’=>’to’).

<!DOCTYPE html><html><head></head>
<body><?php
echo substr_replace("hello world","*",4)."<br />";
echo substr_replace("hello world","*",-5,-2)."<br />";
print_r (substr_replace(["abc","def"],"*",1,1));
echo "<br />";
print_r (substr_replace(["abc","def"],"*",[1,2],1)); 
echo "<br />";
print_r (substr_replace(["abc","def"],"*",1,[1,2])); 
echo "<br />";
print_r (substr_replace(["abc","def"],"*",[0,1],[1,2])); 
echo "<br /><br />";

echo str_replace("l","*","hello world",$i)."---$i<br />";
print_r (str_replace("a","*",["cat","car"])); echo "<br />";
echo str_replace(["ll","rl"],"*","hello world")."<br />";
echo str_replace(["ll","rl"],["**","##"],"hello world")."<br /><br />";

echo strtr("abcdef","ace","XYZ")."<br />";
echo strtr("abcdef",["ab"=>"X","de"=>"MNO"])."<br />";
?></body></html>

hell* hello *ld Array ( [0] => a*c [1] => d*f ) Array ( [0] => a*c [1] => de* ) Array ( [0] => a*c [1] => d* ) Array ( [0] => *bc [1] => d* ) he**o wor*d---3 Array ( [0] => c*t [1] => c*r ) he*o wo*d he**o wo##d XbYdZf XcMNOf