080 - String:ReplaceHello earth. The earth is niceHello . The is niceHi earth. The earth is niceArray
(
[0] => blue
[1] => pink
[2] => green
[3] => yellow
)
Hello rajat. The rajat is nice
Hello ravi
Hello manoj world is nice
Hello vivek nice
santaHello world. The world is nice
Hilla warld. Thi warld is nici
Hi earth. The earth is nice
<?php
// 080 - String:Replace
echo "</br><span class='myheading'>080 - String:Replace</span></br>";
$str = "Hello world. The world is nice";
echo str_replace("world","earth",$str)."</br>"; // replace world by earth
echo str_replace("world","",$str); // replace with blank
$find = ["Hello", "world"];
$replace = ["Hi", "earth"];
print_r (str_replace($find,$replace,$str))."<br>"; // two word same time using array
$color = ["blue","red","green","yellow"];
echo "<pre>";
print_r(str_replace("red","pink",$color)); // replace in array and not in string
echo "</pre>";
echo str_ireplace("WORLD","rajat",$str)."<br>"; // case not matter
echo '<pre>';
print_r( substr_replace($str, "ravi", 6))."<br>"; // form 6th index all string will replaced by ravi
echo '</pre>';
echo '<pre>';
print_r( substr_replace($str, "manoj", 6 , 10))."<br>"; // replace string form 6 to 10th index
echo '</pre>';
echo '<pre>';
print_r( substr_replace($str, "vivek", 6 , -5));
echo '</pre>';
echo '<pre>';
print_r( substr_replace($str, "santa", 0 , 0)); //--- add in starting
echo '</pre>';
echo '<pre>';
print_r(strtr($str,"eo","ia")); // replace e with i and o with a
echo '</pre>';
$array = array("Hello" => "Hi", "world" => "earth");
echo '<pre>';
print_r(strtr($str,$array)); // replace with associated array key
echo '</pre>';
?>