This is what I'm trying to do,
$line = "dsfsdf";
if (!preg_match('/^(?=.{1,30}$)[a-zA-Z0-9\-\_]*$^/', $line))
{
echo 'No Match found';
}
else
{
echo 'Match found';
}
The requirement is below,
- it can have characters
- it can have numbers
- As special character, it can have only hyphen (-) and underscore (_) characters in it
I'm not so good at regex part. Can someone guide me how to achieve it with a simple explanation?
^(start of string anchor) at the end. Try'/^[a-zA-Z0-9_-]{1,30}$/'. Or even'/^[\w-]{1,30}$/'\wmatches what[a-zA-Z0-9_]does if noumodifier is added. Please consider accepting my answer if it works for you.\sstands for whitespace.[\w\s-]will be the character class.