0

Possible Duplicate:
Function to return only alpha-numeric characters from string?

Starting from $string = "hey hello 9times-%&";

I would like to replace all the chars that are NOT numeric[0-9] or [a-z,A-Z] type.

Is there a good method to show this process control?

EDITED

i forgot that i need to leave blank space bar blank spaces, i mean:

"hey &/k" must return as "hey k" and NOT as "heyk"

Community
  • 1
  • 1
itsme
  • 48,972
  • 96
  • 224
  • 345

3 Answers3

1
<?php

$string = "hey hello 9times-%&";
$string = preg_replace('/[^0-9A-Z\s]+/i', '', $string);

echo $string;

?>
Sean Bright
  • 118,630
  • 17
  • 138
  • 146
1

What about preg_replace:

$clean = preg_replace('/[^0-9a-z\s]/i','',$input);
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
1
preg_replace('/[^ \w]+/i', '', $string);

That will work as well. See the codepad example.

hjpotter92
  • 78,589
  • 36
  • 144
  • 183