Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allowing Only Certain Characters In PHP

Tags:

regex

php

I need to check to see if a variable contains anything OTHER than a-z A-Z 0-9 and the "." character (full stop). Any help would be appreciated.

like image 400
zuk1 Avatar asked Dec 28 '08 13:12

zuk1


2 Answers

There are two ways of doing it.

Tell whether the variable contains any one character not in the allowed ranges. This is achieved by using a negative character class [^...]:

preg_match('/[^a-zA-Z0-9\.]/', $your_variable);

Th other alternative is to make sure that every character in the string is in the allowed range:

!preg_match('/^[a-zA-Z0-9\.]*$/', $your_variable);
like image 63
ʞɔıu Avatar answered Sep 30 '22 14:09

ʞɔıu


if (preg_match('/[^A-Z\d.]/i', $var))
  print $var;
like image 42
PEZ Avatar answered Sep 30 '22 16:09

PEZ