You can pass a list of valid characters and tell the function to replace any character that is not in this list:
$str = preg_replace('/[^a-zA-Z0-9*]+/', '', $str);
Square brackets say select everything in this range. Carat ( ^ ) is a regular expression for no. Then we list our valid characters (lowercase from a to z, uppercase from a to z, numbers 0 to 9, and asterisks). A plus sign at the end of the square bracket indicates that 0 or more characters are selected.
Edit:
If this is a list of all the characters you want to keep, then:
$str = preg_replace('/[^ĄąĆćŻżŹźŃńŁłÓó*]+/', '', $str);
source share