I'm new to regex and am trying to match a very simple pattern but am struggling to get regex. I basically just want test a bunch of strings to check if they contain anything that isn't either in 'a-z' (both lower or upper case) or '_' character. So if a string contains the '@' character I want it to fail.
so far it looks like:
if (!preg_match("/[a-z_]/", $string)) {
echo "Bad string";
}
But that's failing to pickup anything it seems. Any help would be greatly appreciated.
Use negation in your regex to search for characters you don't want.
// If the string has any characters other than A-z or _ then echo bad string
if (preg_match("/[^a-zA-Z_]/", $string)) {
echo "Bad string";
}