When the input is given by the user, it is always a good idea to filter and validate the input before processing. You can filter input string as per the required data type with PHP. In this example code, we will show you how to get only alphanumeric characters from string in PHP.
The following code snippet helps you to filter and return only alphanumeric characters from string using PHP.
$string = 'your mixed string here...';
$filtered_string = preg_replace("/[^a-zA-Z0-9]+/", "", $string);
It is working fine. But I want to keep the spaces between words. Is there a way to do that?