Page 1 of 1

Problem using split function with string that includes "&"

Posted: Wed Feb 20, 2008 3:18 pm
by bluefox112
Hi,

First of all, hello everyone. I'm new on this forum, a relatively junior PHP programmer and I have an issue. I did much Googling with no result.

I have a string of this format: aaaaa&aaaa:bbbbb&bbbbbb

I want to split this string into two, separated by the : character.

I used split(":",$string); but I do not get 2 strings, I get 4. The & character seems to mess up the split function.

I get the same thing with preg_split(), and explode()

What should I be doing to handle strings that include the & character?

Regards,

Mike

Re: Problem using split function with string that includes "&"

Posted: Wed Feb 20, 2008 5:17 pm
by markusn00b
I'm unable to replicate the problem. =\

Re: Problem using split function with string that includes "&"

Posted: Thu Feb 21, 2008 8:40 am
by bluefox112
Ok, I tried to simplify my issue, but here it is in it's entirery:

I get a few paramters off the URL, all urlencoded using the urlencode function (and then, of course, decoded).

What is looks like? When encoded: (notice there are colons and semi-colons)

http://192.168.1.1?test=a:b:c:d;e:f:g&:h;

My code now:

$test=urldecode($_GET[test]);
$test_array=split(';',$test);
echo "$test_array[0]<br>";
echo "$test_array[1]<br>";
echo "$test_array[2]<br>";
echo "$test_array[3]<br>";
echo "$test_array[4]<br>";

Instead of getting what I expect (which is two lines, one with "a:b:c:d" and one with "e:f:g&:h", I get this:

a:b:c:d
e:f:g&
:h

So it seems like it's getting messed up by the & character (at least that's what it seems to me).

Using PHP 5.2.3 (can't upgrade or downgrade, stuck with this)

Thanks,

Mike

Re: Problem using split function with string that includes "&"

Posted: Thu Feb 21, 2008 8:46 am
by Zoxive
Works for me

Code: Select all

kyle@w3jubuntu:~$ php5 -r "
> var_dump(explode(':','aaaaa&aaaa:bbbbb&bbbbbb'));
> ";
array(2) {
  [0]=>
  string(10) "aaaaa&aaaa"
  [1]=>
  string(12) "bbbbb&bbbbbb"
}
Try var_dump() and print_r instead of just echo, so you can see what the explode function is giving you to work with.