This if condition:
if (preg_match('/^[a-zA-Z_]+\z/', $network_path))
Skips strings like: bla-bla-bla-bla
How can I Improve this regex so it would accept strings like above.. ?
This if condition:
if (preg_match('/^[a-zA-Z_]+\z/', $network_path))
Skips strings like: bla-bla-bla-bla
How can I Improve this regex so it would accept strings like above.. ?
This [a-zA-Z_]
is a character class. This construct matches one character from those defined inside the square brackets.
So this class matches a to z, A to Z and the underscore. If you want to match also a dash, you just need to add it to the class. But be careful, -
is a special character inside a character class, so if you want to match it literally you need to escape it or put it to the start or the end of the class.
if (preg_match('/^[-a-zA-Z_]+\z/', $network_path))
or
if (preg_match('/^[a-zA-Z_-]+\z/', $network_path))
or
if (preg_match('/^[a-zA-Z\-_]+\z/', $network_path))