Page 1 of 1

ereg() Alpha Numeric only...Except for the _ and space

Posted: Fri Jul 16, 2004 7:53 am
by hawleyjr
I'm using the following ereg() expression to determine if the string is only alpha or numeric.

Code: Select all

<?php
ereg('[^A-Za-z0-9]', $dirName)
?>
Instead of returning true if the string is only alpha or numeric. How do I make it return true if the string is only alpha, numeric, an underscore or a space?

Posted: Fri Jul 16, 2004 8:06 am
by JayBird
it may be better to use the built in PHP functions for these tasks instead of ereg

http://se.php.net/manual/en/function.ctype-alnum.php

Mark

Posted: Fri Jul 16, 2004 8:12 am
by hawleyjr
Cheers Bech!

Posted: Fri Jul 16, 2004 10:51 am
by feyd
if you want to allow empty strings:

Code: Select all

preg_match('#^[A-Za-z0-9_ ]*$#',$string)
if you don't:

Code: Select all

preg_match('#^[A-Za-z0-9_ ]+$#',$string)