What is the purpose of the preg_replace function in PHP and how is it used in the provided code snippet?
The purpose of the preg_replace function in PHP is to perform a search and replace using a regular expression. In the provided code snippet, the preg_replace function is used to remove all non-alphanumeric characters from a string. This is achieved by specifying a regular expression pattern that matches any character that is not a letter or a number, and replacing it with an empty string.
$string = "Hello, World! 123";
$clean_string = preg_replace("/[^A-Za-z0-9]/", "", $string);
echo $clean_string;