Extracting part of a string before colon

Discussion in 'PHP' started by mnymkr, Jan 10, 2009.

  1. #1
    I need to extract numbers that appear before a colon in a string

    for instance:

    14:hey-the-bear

    or

    1432:happy-candy

    I need to grab the 14 or 1432
     
    mnymkr, Jan 10, 2009 IP
  2. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #2
    A tough one... or not.

    $string = '1432:happy-candy';
    echo (int) $string;
    // Can use typecasting
    
    echo ' ' , PHP_EOL;
    
    echo intval('14:hey-the-bear');
    // Or intval, or any other similar function/cast that does the same thing.
    PHP:
    Dan.
     
    Danltn, Jan 10, 2009 IP
  3. french-webbie

    french-webbie Peon

    Messages:
    194
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Or you can do it with regular expressions.

    $string = '1432:happy-candy';
    preg_match('~(\d+)\:~', $string, $matches);
    if (isset($matches[1])) {
    	// $matches[1] is what you're looking for
    }
    
    PHP:
     
    french-webbie, Jan 10, 2009 IP
  4. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #4
    I was waiting for this so I could criticize... You should only use the Regular expression engine when it's not possible using a single function call, as initializing it is VERY slow (relatively.)

    I would post a normal functional way but I'm drunk atm, so I'm thinking better of it...

    Dan.
     
    Danltn, Jan 10, 2009 IP
  5. Amator

    Amator Well-Known Member

    Messages:
    1,424
    Likes Received:
    55
    Best Answers:
    0
    Trophy Points:
    165
    #5
    or using the strstr() function
    $string = '1432:happy-candy';
    $number = strstr($string, ':', true);
     
    Amator, Jan 10, 2009 IP
  6. ez-designs

    ez-designs Well-Known Member

    Messages:
    230
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #6
    You can try this:

    
    $string = "14:hey-the-bear";
    echo strstr($string, ':', true);
    
    //should return the number 14
    
    PHP:
     
    ez-designs, Jan 10, 2009 IP
  7. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #7
    To the last 2 posts, that will only work on PHP 5.3+ - I'm all for using it, but fails miserably for portability.

    Dan.
     
    Danltn, Jan 10, 2009 IP
  8. harrisunderwork

    harrisunderwork Well-Known Member

    Messages:
    1,005
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    135
    #8
    I assume that the string is of the only format you have given and the number is BEFORE the colon.

    
    
    $parts = explode (':',$string);
    $number = $parts[0];
    if(is_numeric($number))
    {
    print $number;
    }
    
    
    Code (markup):
    This would perfectly according to info given by you.
     
    harrisunderwork, Jan 10, 2009 IP
  9. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #9
    Explode is what I would have suggested. I know it is good from PHP4 and up.
     
    Colbyt, Jan 10, 2009 IP
  10. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #10
    wow really great posts, which of these would be best insert the number directly into a query
     
    mnymkr, Jan 10, 2009 IP
  11. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #11
    My original IMO (but I'm biased.)

    Fastest and defaults to '0' if there is no number.
     
    Danltn, Jan 10, 2009 IP
  12. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #12
    i am sort of confused by your code though, which part of it do i use

    was it all one suggestion?
     
    mnymkr, Jan 10, 2009 IP
  13. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #13
    intval($string);
    OR
    (int) $string;

    One is a function, one is a typecast.
     
    Danltn, Jan 10, 2009 IP