Page 1 of 1

testing whether a value is alpha-numeric

Posted: Wed Jun 07, 2006 6:29 pm
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?

Posted: Wed Jun 07, 2006 6:32 pm
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";    
}

?>

Posted: Wed Jun 07, 2006 6:37 pm
by Christopher
Either regular expressions like the preg_ or ereg_ functions, or the ctype_ functions will work.