What would be the easiest way to strip a string down to just alphanumerics - no spaces... (for a username / password database).
Currently, i use ereg_replace(); to remove the whitespace. Is there a built in function or 'easier' way to check for / remove all other characters in a string without doing a search and replace for each one seperately ?
Thanks !
I'd use preg_replace, but it's the same idea as ereg_replace...so no, there's no easier way.
Diego
Maybe Preg? This will replace all ALPHA characters, A-Z, upper and lowercase...
$pattern = "/[A-Za-z/g"; $replace = ""; $string = "Just some test... 45 test 455"; $string = preg_replace($pattern, $replace, $string); echo $string;
HOLD ON! Messed up... hehe the pattern should be...
/[A-Za-z]/
DROP THAT g at the end. 🙂
thanks!
convienent for removing all alpha characters, but i want to <B>keep</B> only alpha characters and remove all others....
you can negate a character class (that definition in square brackets) by inserting ^ at the beginning, like
// strip off everthing that is not listed within the brackets $str = preg_replace('/[^a-zA-Z0-9]/', '', $str);
[edit: typo]
thanks so much ! i knew there had to be an easier way, there's a reason i've chosen to learn php rather than another language. 😉