Allow only [a-z][A-Z][0-9] in string using PHP

You can use the preg_replace function in PHP to remove any characters that do not match the specified pattern. To allow only lowercase letters, uppercase letters, and digits in a string, you can use the following code:

<?php

$string = "Hello World! 123";

$string = preg_replace("/[^a-zA-Z0-9]/", "", $string);

echo $string;

// Output:
// HelloWorld123

?>

Watch a course Learn object oriented PHP

This will remove all characters that are not lowercase letters, uppercase letters, or digits from the $string variable.