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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

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

Post 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?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Cheers Bech!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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)
Post Reply