How can the str_replace function in PHP be utilized to remove specific elements from a string?

To remove specific elements from a string using the str_replace function in PHP, you can pass an array of elements to be removed as the search parameter and an empty string as the replacement parameter. This will effectively remove the specified elements from the string.

$string = "Hello, World! This is a sample string.";
$elements_to_remove = array("Hello", "sample");
$new_string = str_replace($elements_to_remove, "", $string);

echo $new_string; // Output: , World! This is a string.