What is the purpose of the preg_replace function in the provided PHP code?
The purpose of the preg_replace function in the provided PHP code is to replace any non-alphanumeric characters in a string with an underscore. This is commonly used to sanitize input data or format strings for specific requirements.
// Replace non-alphanumeric characters with an underscore
$string = "Hello, World! This is a test string.";
$cleaned_string = preg_replace('/[^a-zA-Z0-9]/', '_', $string);
echo $cleaned_string;