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
Problem using split function with string that includes "&"
Moderator: General Moderators
-
bluefox112
- Forum Newbie
- Posts: 2
- Joined: Wed Feb 20, 2008 3:15 pm
- markusn00b
- Forum Contributor
- Posts: 298
- Joined: Sat Oct 20, 2007 2:16 pm
- Location: York, England
Re: Problem using split function with string that includes "&"
I'm unable to replicate the problem. =\
-
bluefox112
- Forum Newbie
- Posts: 2
- Joined: Wed Feb 20, 2008 3:15 pm
Re: Problem using split function with string that includes "&"
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
c:d" and one with "e:f:g&:h", I get this:
a
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
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
a
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 "&"
Works for me
Try var_dump() and print_r instead of just echo, so you can see what the explode function is giving you to work with.
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"
}