Returning the Value of a part of a variable (solved)

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
Bigun
Forum Contributor
Posts: 237
Joined: Tue Jun 13, 2006 10:50 am

Returning the Value of a part of a variable (solved)

Post by Bigun »

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.
Bigun
Forum Contributor
Posts: 237
Joined: Tue Jun 13, 2006 10:50 am

Post by Bigun »

It returns the variable type, but not the character type.

I would like to distinguish the difference between:

"B-11234"

and

"11234"

I'm trying to detect if the first character in the variable is a letter... specifically "B".
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

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;
  }
}
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

I like that one better :wink:
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Geshi eats character classes like this: [[:alpha:]]
Bigun
Forum Contributor
Posts: 237
Joined: Tue Jun 13, 2006 10:50 am

Post by Bigun »

Worked like a charm, thanks...
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

astions 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 think you'll make use of the range() function ;)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

range() returns an array of elements from low to high, inclusive. If low > high, the sequence will be from high to low.
I dig it. Thanks!
Post Reply