testing whether a value is alpha-numeric

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
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

testing whether a value is alpha-numeric

Post by Luke »

Is there a function that tests whether a value is an alpha-numeric string? I looked on php.net but couldn't find one. Am I going to have to use regex?
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

oh google how I love thee

http://www.totallyphp.co.uk/code/check_ ... g_ereg.htm

Code: Select all

<?php

// Example 1

$text = "onlyalphanumericcharacters012345";

if (ereg('[^A-Za-z0-9]', $text)) {
  echo "This contains characters other than letters and numbers";
}
else {
  echo "This contains only letters and numbers";    
}

// Example 2

$text = "mixedcharacters012345&../@";

if (ereg('[^A-Za-z0-9]', $text)) {
  echo "This contains characters other than letters and numbers";
}
else {
  echo "This contains only letters and numbers";    
}

?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Either regular expressions like the preg_ or ereg_ functions, or the ctype_ functions will work.
(#10850)
Post Reply