Seperate number from end of url

Discussion in 'PHP' started by frobak, Dec 9, 2011.

  1. #1
    frobak, Dec 9, 2011 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    why not use php.net/explode and then use the last item of you array?
     
    EricBruggema, Dec 9, 2011 IP
  3. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #3
    Since this is the PHP forum
    
    $array = explode('/', $url); //split the pieces
    $count = count($array); //find out how many there are
    $var2 = $array($count - 1); //the last one is the number
    unset($array[$count - 1]); //get rid of the number
    $var1 = implode('/', $array); //the remaining array holds the URL
    
    PHP:
    (This is untested code, so use it as a starting point.)
     
    Rukbat, Dec 10, 2011 IP
  4. NIKOLO

    NIKOLO Member

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    46
    #4
    <?php
    
    $url = "www.something.com/something/10001";
    
    $exp = explode("/", $url);
    $num_len = strlen($exp[count($exp)-1]);
    $url_len = strlen($url)-$num_len;
    
    $var1 = substr($url, 0, $url_len); 
    $var2 = substr($url, $url_len, $num_len);
    
    ?>
    Code (markup):
    Tested and working :)
     
    NIKOLO, Dec 11, 2011 IP