How can the str_replace and strtr functions be used to modify strings in PHP?
The str_replace function in PHP can be used to replace specific substrings within a string with another substring. On the other hand, the strtr function can be used to replace characters in a string based on a mapping provided by the user. By using these functions, you can easily modify strings in PHP by replacing specific characters or substrings with desired values.
// Using str_replace to replace 'old' with 'new' in a string
$string = "This is an example string.";
$new_string = str_replace("example", "modified", $string);
echo $new_string;
// Using strtr to replace characters based on a mapping
$trans = array("h" => "H", "s" => "S");
$string = "this is a sample string";
$new_string = strtr($string, $trans);
echo $new_string;