Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Apr 2006
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Splitting a string into 2 variables

    Hello.

    I'm having trouble splitting a string into 2 variables in the following situation:

    an outside script from an affiliated site is going to post to my script using GET method, they will give me a numeric string and unfortunately, I cannot modify their script.

    The string will be posted using the get method and named 'sid'

    so for example, it may be ?sid= 1009102

    Now here's what I want to do:

    I need to "grab" the first 4 digits, and pass them into one variable.

    There may be any number of digits after that, I'll need to pass those digits into a second variable.

    For the above example, I'd need it to pass into the following:

    $variable1 = 1009
    $variable2 = 102

    However, should there be more digits, such as in this example, I'll need to tack them onto the end of $variable2 :

    ?sid=101020000

    $variable1 = 1010
    $variable2 = 20000

    In other words, the first four digits will always be passed into $variable1 and anything else will be passed into $variable2

    I've never done anything of this nature before so any help is appreciated.

    Thanks!

  • #2
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    How about this?
    Code:
    $var = '1009102';
    $variable1 = substr($var, 0, 4);
    $variable2 = substr($var, 4);

  • #3
    New Coder
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    49
    Thanks
    0
    Thanked 3 Times in 3 Posts
    or like this

    PHP Code:
    $var '101020000';
    list(
    $var1$var2) = explode('|'substr_replace($var'|'40)); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •