View Full Version : Splitting a string into 2 variables

01-02-2007, 10:30 AM

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 :


$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.


01-02-2007, 10:49 AM
How about this?

$var = '1009102';
$variable1 = substr($var, 0, 4);
$variable2 = substr($var, 4);

01-02-2007, 11:38 PM
or like this

$var = '101020000';
list($var1, $var2) = explode('|', substr_replace($var, '|', 4, 0));