How to convert char numbers to int numbers

Discussion in 'PHP' started by AHA7, Oct 12, 2007.

  1. #1
    Hello,

    Is there anyway I can convert character numbers (matched by a preg_match and stored into a string) into integer numbers so that I can compare them (==, >, <,...) with other integer variables?
     
    AHA7, Oct 12, 2007 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    
    
    $chars = $result_of_preg_match; //EX string = 45309238
    
    $ints = (int)$chars; //OUTPUT = int 45309238
    
    
    PHP:
     
    jestep, Oct 12, 2007 IP
  3. shahcloud

    shahcloud Peon

    Messages:
    95
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks! Thats help me too :D
     
    shahcloud, Feb 9, 2008 IP
  4. The Critic

    The Critic Peon

    Messages:
    392
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Or:
    
    $chars = $result_of_preg_match; //EX string = 45309238
    $ints = $chars+0; //OUTPUT = int 45309238
    
    PHP:
     
    The Critic, Feb 9, 2008 IP
  5. barts2108

    barts2108 Guest

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    In one line this should work (if you do not need the $chars)

    $ints = intval($result_of_preg_match);
    Code (markup):
    intval() can also convert from other 'base' for example Hexadecimal.

    have a look here http://nl.php.net/manual/en/function.intval.php for details
     
    barts2108, Feb 9, 2008 IP