SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict mmelen's Avatar
    Join Date
    Jul 2005
    Location
    NJ/NY USA
    Posts
    243
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    If the first letter is...

    How do I write the code to say... If the first letter of the string is G then....

    thanks in advance!

  2. #2
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if (strpos($str,'G') === 0doSomething(); 
    If you want to match both lowercase and uppercase, use stripos() instead.

  3. #3
    SitePoint Evangelist Tecknowjnkie's Avatar
    Join Date
    Nov 2004
    Location
    Mission Viejo
    Posts
    403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if(preg_match('/^G.?/i',$str)) {
          
    $do some stuff;

    Another alternative and I believe this would be faster then strpos function if speed is necessary.

  4. #4
    SitePoint Addict mmelen's Avatar
    Join Date
    Jul 2005
    Location
    NJ/NY USA
    Posts
    243
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hm... i'll try both and see.

    thanks!

  5. #5
    SitePoint Guru
    Join Date
    Jul 2005
    Location
    Orlando
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    switch( strtolowersubstr$string0) ) )
    {
      case 
    a::
      case 
    b:
      ...
      case 
    g:
      ...


  6. #6
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You can also reference characters in a string similar to values in arrays so $string[0] is the first character of $string.
    - Robert


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •