What are the advantages of using preg_replace over multiple str_replace and preg_replace functions to remove spaces, line breaks, and   from strings in PHP?

When dealing with removing spaces, line breaks, and   from strings in PHP, using preg_replace offers a more efficient and concise solution compared to using multiple str_replace and preg_replace functions. Preg_replace allows for the use of regular expressions to target and replace multiple patterns in a single function call, reducing the need for multiple function calls and increasing code readability.

$string = "Hello   World\n";
$cleaned_string = preg_replace('/\s+| /', '', $string);
echo $cleaned_string;