Problem using split function with string that includes "&"

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bluefox112
Forum Newbie
Posts: 2
Joined: Wed Feb 20, 2008 3:15 pm

Problem using split function with string that includes "&"

Post 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
User avatar
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 "&"

Post by markusn00b »

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 "&"

Post 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
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

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

Post 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.
Post Reply