How to Get only Alphanumeric Characters from String in PHP

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.

  • Use PHP preg_replace() function to remove everything except a-z, A-Z, and 0-9 from the string.
$string 'your mixed string here...'; 

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

RELATED HOW TO GUIDES

1 Comment

  1. Sundar Sau Said...

Leave a reply

keyboard_double_arrow_up