What are some best practices for handling character replacement and string manipulation in PHP functions like hide_letter()?

When handling character replacement and string manipulation in PHP functions like hide_letter(), it is important to use built-in functions like str_replace() or preg_replace() to efficiently replace characters in a string. Additionally, consider using regular expressions to target specific patterns for replacement. It is also good practice to sanitize input data to prevent any security vulnerabilities.

function hide_letter($string, $letter) {
    $hidden_string = str_replace($letter, '*', $string);
    return $hidden_string;
}

// Example usage
$string = "Hello World";
$hidden_string = hide_letter($string, 'o');
echo $hidden_string; // Output: Hell* W*rld