SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist Tapan's Avatar
    Join Date
    May 2005
    Location
    India
    Posts
    564
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    How to find first available alphabet in a given string ?

    Hi,

    How to get position of first available alphabet in a given string ?
    Eg. string: 9A1234

    So it should return 2 (that A in on the 2nd position in the given string.)

    Thanks.

  2. #2
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,322
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Tapan View Post
    Hi,

    How to get position of first available alphabet in a given string ?
    Eg. string: 9A1234

    So it should return 2 (that A in on the 2nd position in the given string.)

    Thanks.
    Code:
    $string = '9A1234';
    
    
    $alpha_found = preg_match('/[a-zA-Z]/', $string, $match, PREG_OFFSET_CAPTURE);
    
    
    $pos = NULL;
    if( $alpha_found ) {
        $pos = $match[0][1]+1;
    }

  3. #3
    SitePoint Member belansus's Avatar
    Join Date
    Aug 2013
    Location
    Newcastle
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    strpos

    PHP Code:
    $haystack'9A1234';
    $needle'A';
    $pos strpos($haystack$needle); 
    Hope this helps


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
  •