How can I find a point in a string where numbers start and end?

Discussion in 'PHP' started by -bank-, May 31, 2008.

  1. #1
    Hi Guys and Girls,

    I'm currently trying to make a script that just decodes strings to get two values, so I need to find where they start and where they end, preferably I'd like to create an array with them so for example number 1 would be $num[0] and number 2 $num[1].

    The strings all vary, so I can't explode them, information like:

    05place11time
    03location6seconds
    8ct5tm

    Although, there tends to be information before and after those, and ideas or functions I can use to get them out? I thought I could find the first position for a number, the last position, then remove all the characters though some how break them apart? Any other ideas? Or solutions?

    Thanks
     
    -bank-, May 31, 2008 IP
  2. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #2
    I don't quiet understand what you need. However, I know strings can be broken using substr. This function will return an array of the string:

    
    function makearray($string){
       $array_let = array();
       $x=0;
       $len = strlen($string);
       while($x <= $len){
          array_push($array_let,substr($string,$x,1));
          $x++;
        }
       return $array_let;
    }
    
    PHP:
     
    Barti1987, May 31, 2008 IP
  3. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #3
    I don't quiet understand what you need. However, I know strings can be broken using substr. This function will return an array of the string:

    
    function makearray($string){
       $array_let = array();
       $x=0;
       $len = strlen($string);
       while($x <= $len){
          array_push($array_let,substr($string,$x,1));
          $x++;
        }
       return $array_let;
    }
    
    PHP:
     
    Barti1987, May 31, 2008 IP
  4. Dagon

    Dagon Active Member

    Messages:
    122
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    60
    #4
    preg_match("/([a-zA-Z]*)([0-9]*)([a-zA-Z]*)([0-9]*)/", $string, $matches);

    $matches will return an array containing:
    element 1 = the first text part
    element 2 = the first numeric part
    element 3 = the second text part
    element 4 = the second numeric part
     
    Dagon, May 31, 2008 IP
  5. -bank-

    -bank- Well-Known Member

    Messages:
    674
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    120
    #5
    Thanks alot both of you, I will use that code dagon just posted, looks like it will do the trick :)
     
    -bank-, May 31, 2008 IP