Returning the Value of a part of a variable (solved)
Moderator: General Moderators
Returning the Value of a part of a variable (solved)
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?
Last edited by Bigun on Mon Jun 19, 2006 2:08 pm, edited 1 time in total.
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);
}
Last edited by Weirdan on Mon Jun 19, 2006 1:21 pm, edited 2 times in total.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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; } }