Returning the Value of a part of a variable (solved)
Posted: Mon Jun 19, 2006 12:21 pm
I'd like to have PHP look over the 1st character of a variable, then be able to determine if it is a number or a letter. How would that code look?
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
function FirstCharacterIsLetter($String) {
$String = substr($String, 0, 1);
$String = strtolower($String);
$Letters = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
if (!in_array($String,$Letters)) {
return false;
} else {
return true;
}
}Code: Select all
function firstCharIsLetter($string) {
return ereg('^[[:alpha:]]', $string);
}I think you'll make use of the range() functionastions wrote:Code: Select all
function FirstCharacterIsLetter($String) { $String = substr($String, 0, 1); $String = strtolower($String); $Letters = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'); if (!in_array($String,$Letters)) { return false; } else { return true; } }
I dig it. Thanks!range() returns an array of elements from low to high, inclusive. If low > high, the sequence will be from high to low.