Can someone tell me how to remove anything from a variable except for letters, numbers, or spaces? For example, if it has & or dashes or anything else like that i don't want it to remain in the string.
| SitePoint Sponsor |





Can someone tell me how to remove anything from a variable except for letters, numbers, or spaces? For example, if it has & or dashes or anything else like that i don't want it to remain in the string.
John Saunders



I know this isnt what you asked for, but heres the regex for checking if the variable only has letters, numbers or spaces in it:
PHP Code:if (preg_match("/[^a-zA-Z0-9 ]/",$var)) {
echo 'You may only use letters, numbers and spaces!';
} else {
echo 'Heres a cookie';
}
Or can change that to
PHP Code:$data = preg_replace('/[^a-z0-9 ]/i', '', $data);
Bookmarks