How can regular expressions be used to manipulate strings in PHP, such as removing unwanted variables?

Regular expressions can be used in PHP to manipulate strings by matching patterns within the strings. To remove unwanted variables from a string, you can use the `preg_replace()` function to search for specific patterns and replace them with an empty string. By defining the pattern that represents the unwanted variables, you can easily remove them from the string.

// Example of removing unwanted variables from a string using regular expressions
$string = "This is a string with unwanted variables: $var1, $var2, $var3";

// Define the pattern to match unwanted variables (e.g. $var1, $var2, $var3)
$pattern = '/\$\w+/';

// Replace unwanted variables with an empty string
$cleaned_string = preg_replace($pattern, '', $string);

echo $cleaned_string;