Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

preg_replace regular expression issue

I'm trying 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);
like image 726
user614088 Avatar asked Apr 12 '26 07:04

user614088


1 Answers

Try

$str = preg_replace('/[^\w\r\n\t+*()[\]!#?.\,;:\'"<> -]/', '', $str);
like image 61
YOU Avatar answered Apr 14 '26 19:04

YOU



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!