42

I'd like a regexp or other string which can replace everything except alphanumeric chars (a-z and 0-9) from a string. All things such as ,@#$(@*810 should be stripped. Any ideas?

Edit: I now need this to strip everything but allow dots, so everything but a-z, 1-9, .. Ideas?

Ali
  • 261,656
  • 265
  • 575
  • 769
  • When you say "strip everything", you don't want to map Unicode to nearest ASCII ("ASCII hammer")? So you're ok with *Münchener Straße* -> *Mnchener Strae* and not *Muenchener Strasse*? – smci Apr 17 '23 at 01:00

5 Answers5

73
$string = preg_replace("/[^a-z0-9.]+/i", "", $string);

Matches one or more characters not a-z 0-9 [case-insensitive], or "." and replaces with ""

gnarf
  • 105,192
  • 25
  • 127
  • 161
  • How can i change that to allow only dots (i.e .)? – Ali May 09 '09 at 09:34
  • 1
    You don’t need to escape the dot inside the character set. – Gumbo May 10 '09 at 10:04
  • Do you really need the `+` sign? Someone posted below without the `+` sign. – kuchi Mar 31 '12 at 07:22
  • 3
    @kuchi: very old question but i just came across it: it does NOT make a difference if you replace with an empty string. it DOES make a difference if you replace with something else, say a dash (`"-"`). With the `+` it will change `"test$!§test"` to `"test-test"`. Without the `"+"` it gets changed to `"test---test"` – Jörn Berkefeld Jun 04 '15 at 12:08
21

I like using [^[:alnum:]] for this, less room for error.

preg_replace('/[^[:alnum:]]/', '', "(ABC)-[123]"); // returns 'ABC123'
Corban Brook
  • 21,270
  • 4
  • 28
  • 34
2
/[^a-z0-9.]/

should do the trick

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
2

Try:

$string = preg_replace ('/[^a-z0-9]/i', '', $string);

/i stands for case insensitivity (if you need it, of course).

Ilya Birman
  • 9,834
  • 4
  • 27
  • 31
1

This also works to replace anything not a digit, a word character, or a period with an underscore. Useful for filenames.

$clean = preg_replace('/[^\d\w.]+/', '_', $string);
Peter Gluck
  • 8,168
  • 1
  • 38
  • 37