What is the regular expression for this condition?

I want the regex to deactivate my data, which must meet the following condition

a) a-z and A-Z allowed

b) 0-9 allowed

c) Special Symbols like Comma (,) dot (.) question Mark (? allowed)

d) Single Space is allowed

I tried and came up with this

preg_replace('%[^a-zA-Z0-9,.?\s]%', '', $string);

I am not very familiar with RegExp, although the code above works, I would like to know

a) if I use the correct RegExp syntax?

b) if I do not use modulus(%)at the beginning and end of the syntax there will be no work, and I have no idea what the purpose of the module is here?

+3
source share
4 answers

a) If it works, it is correct.

b) " PCRE , . , , " - PHP

+1

whitespace ( , , , , ..), . , \s . preg_replace .

% preg_replace . preg_ , . , .

+2

\ s is a space character. Which includes tabs.

Also, if you can enable underscores, \ w can make it a little easier.

\w = [a-zA-Z0-9_]   // \w is a "word" character (including underscores)
+1
source

You need to avoid periods, semicolons and question marks

[^a-zA-Z0-9\,\.\?\s]

0
source

All Articles