I'm trying 开发者_JS百科to clean up a string.
I need to only allow
- Uppercase/lower letters
- numbers
- spaces, tabs, carriage returns
- these characters:
_-+*()[]!#?.,;:'"<>
Everything else needs to go bye-bye. How do I go about this? I have this, which works for the upper/lower case letters, numbers, and spaces. But I dont know how to account for the tabs, carriage returns, or how to do the special characters?
$str = preg_replace('/[^a-z0-9 ]/i', '', $str);
Try
$str = preg_replace('/[^\w\r\n\t+*()[\]!#?.\,;:\'"<> -]/', '', $str);
\s - whitespace character (includes tabs and line breaks)
\r - carriage return
Use "\" character for special symobls.
$str = 'sample|';
$result = !(bool)strlen(preg_replace('/^[a-z0-9A-Z\r\s:_-+*()[]!#?.,;:\>\<]*/', '', $str))
精彩评论