How can regular expressions be used to remove spaces, line breaks, and   from strings in PHP?

Regular expressions can be used in PHP to remove spaces, line breaks, and non-breaking spaces ( ) from strings by using the preg_replace function. By specifying the patterns for spaces, line breaks, and   in the regular expression, we can easily remove them from the input string.

$input_string = "This is a string with spaces, line breaks, and  ";
$output_string = preg_replace('/\s+| /', '', $input_string);
echo $output_string;