I need to write a preg_match function that checks the values of an input. The input will essentially be a name, but I want to allow it to have letters, numbers, and any non-dangerous symbols like dashes, commas, periods, and ampersands. How do I do that?
if (preg_match('#^[a-z0-9\-_,\.&]+$#iD', trim($name))) { //valid } else { //invalid } PHP: That would assume if the $name contains only those characters between the [...] (take no notice of the backslashes) and is alteast 1 character to be valid.
Would the following then allow parentheses, tildes, and that funny apostrophe under the tilde? if (preg_match('#^[a-z0-9\-_,\(\)\~\`\.&]+$#iD', trim($name))) { //valid } else { //invalid } PHP:
if (preg_match('#^[a-z0-9\-_,\.&\(\)~` ]+$#iD', trim($name))) { //valid } else { //invalid } PHP: Use that, you don't need to backslash/escape every character but only specific characters (this can be confusing at first but don't worry) - the specific characters are the following (they've been seperated with a space for readability): . \ + * ? [ ^ ] $ ( ) { } = ! < > | : - Theirfore if any of the above are used add a backslash before the character example; if I was to use the question mark; \?