Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check a variable using regex

Tags:

regex

php

Im about to create a registration form for my website. I need to check the variable, and accept it only if contains letter, number, _ or -.

How can do it with regex? I used to work with them with preg_replace(), but i think this is not the case. Also, i know that the "ereg" function is dead. Any solutions?

like image 997
markzzz Avatar asked Sep 10 '10 13:09

markzzz


1 Answers

this regex is pretty common these days.

if(preg_match('/^[a-z0-9\-\_]+$/i',$username))
{
   // Ok
}
like image 142
RobertPitt Avatar answered Sep 30 '22 06:09

RobertPitt