Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing non-alphanumeric characters from a string

I have a string in PHP and I want it to match the regex [A-Za-Z0-9]. How can I do this?

like image 657
Malfist Avatar asked Mar 30 '10 19:03

Malfist


1 Answers

I am assuming you meant, a-z instead of a-Z, inside of your regex, but you can use preg_replace

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

It takes as arguments the pattern ([a-zA-Z0-9]), replacement ("") and the subject ($string) and returns the new string ($new_string)

like image 90
Anthony Forloney Avatar answered Sep 22 '22 02:09

Anthony Forloney