Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

preg_match only letters, spaces and dashes and spaces allowed

Tags:

php

i know that one condition that im currently using where it gives an error if $stringabc contains anything but numbers is :

if(preg_match("/[^0-9]/",$stringabc))

I want an if condition where it gives an error if $stringdef contains anything but letters, spaces and dashes (-).

like image 859
Alexand657 Avatar asked Dec 12 '12 00:12

Alexand657


2 Answers

That would be:

if(preg_match('/[^a-z\s-]/i',$stringabc))

for "anything but letters (a-z), spaces (\s, meaning any kind of whitespace), and dashes (-)".

To also allow numbers:

if(preg_match('/[^0-9a-z\s-]/i',$stringabc))
like image 94
mvds Avatar answered Nov 14 '22 23:11

mvds


You can use something like:

preg_match("/[^a-z0-9 -]/i", $stringabc)
like image 28
jeroen Avatar answered Nov 14 '22 23:11

jeroen