Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all but valid characters

Tags:

regex

php

Valid characters include the alphabet (abcd..), numbers (0123456789), spaces, ' and ".

I need to strip any other characters than these from a string in PHP.

Thanks :)

like image 614
Matt Avatar asked Aug 17 '10 16:08

Matt


1 Answers

You can do this:

$str = preg_replace('/[^a-z0-9 "\']/', '', $str);

Here the character class [^a-z0-9 "'] will match any character except the listed ones (note the inverting ^ at the begin of the character class) that are then replaced by an empty string.

like image 130
Gumbo Avatar answered Sep 18 '22 12:09

Gumbo