开发者

preg_replace regular expression issue

开发者 https://www.devze.com 2023-02-10 06:05 出处:网络
I\'m trying 开发者_JS百科to clean up a string. I need to only allow Uppercase/lower letters numbers

I'm trying 开发者_JS百科to clean up a string.

I need to only allow

  1. Uppercase/lower letters
  2. numbers
  3. spaces, tabs, carriage returns
  4. 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))

0

精彩评论

暂无评论...
验证码 换一张
取 消