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
  • 47,321
  • 93
  • 214
  • 341

3 Answers3

1
<?php

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

echo $string;

?>
Sean Bright
  • 114,945
  • 17
  • 134
  • 143
1

What about preg_replace:

$clean = preg_replace('/[^0-9a-z\s]/i','',$input);
Elias Van Ootegem
  • 70,983
  • 9
  • 108
  • 145
1
preg_replace('/[^ \w]+/i', '', $string);

That will work as well. See the codepad example.

hjpotter92
  • 75,209
  • 33
  • 136
  • 171