Separte parts of a string

Discussion in 'PHP' started by heyman12, Apr 9, 2009.

  1. #1
    I am working on script to deal with strings, and I need it to take strings such as what you see below, and separate it out as seen below that.

    $string_A = "some-random-text-t9958302"
    $string_B = "unimportant-text-f100394"
    $string_C = "nothing-here-matters-at-all-m4954433"

    I am trying to pull the portion after the last - out of it, and separate it.

    $string_A[0] = "t"
    $string_A[1] = "9958302"

    $string_B[0] = "f"
    $string_B[1] = "100394"

    $string_C[0] = "m"
    $string_C[1] = "4954433"

    For some reason this last left me stumped...

    Any Ideas?
     
    heyman12, Apr 9, 2009 IP
  2. rob_v

    rob_v Peon

    Messages:
    72
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $string_A = "some-random-text-t9958302"
    $explode_array = explode('-',$string_A);
    $last_element = end($explode);
    $string_array_a[0] = substr($last_element,0);
    $string_array_a[1] = substr($last_element, 1);


    That should do it.
    I mean thats the basic logic to use - just massage it a bit to suite your needs ;)
     
    rob_v, Apr 9, 2009 IP
  3. heyman12

    heyman12 Peon

    Messages:
    56
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Many thanks, I just adapted it what I am using it for and it works perfectly!
     
    heyman12, Apr 9, 2009 IP
  4. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Another approach:

    preg_match('/-([a-z])(\d+)$/', $string_A, $matches);
    $array_shift($matches);
    $string_A = $matches;
    Code (markup):
     
    SmallPotatoes, Apr 9, 2009 IP