Page 1 of 1

explode() not working

Posted: Sat Nov 12, 2011 12:11 am
by tangibleLime
I have a function that takes in a string of the form "x|y|z...". In that function, I use explode to store it into an array.

Code: Select all

function test($idList) {
$idArray = explode("|",$idList);
echo $idArray[0];
}
This function is called from another file...

Code: Select all

.
.
.
echo $values['userids'];   // displays "1|2"
test($values['userids']);   // displays "1|2"... why?
.
.
.
If I modify the function and hard-code the parameter value like this,

Code: Select all

function test($idList) {
$idList = "1|2";
$idArray = explode("|",$idList);
echo $idArray[0];
}
Or in the calling code,

Code: Select all

.
.
.
echo $values['userids'];   // displays "1|2"
test("1|2");   // displays "1"
.
.
.
Then it suddenly works. What's going on?

If I go back into the function and (when it's fed "1|2") I use a conditional like this...

Code: Select all

echo $idList;  // displays "1|2"
if ($idList == "1|2") {   // apparently false??
   die("fail");  // never reached??
}
It never hits the die() statement.

Re: explode() not working

Posted: Sat Nov 12, 2011 3:20 am
by mikeashfield
Try this:

Code: Select all

<?php
	$idList = $REQUEST['idList'];
	echo '$idList';
	function explodeVar($List) {
		global $idArray = explode('|', $List)
	}
	explodeVar($idList);
	foreach ($idArray as $id) {
		echo '$id' . "\n";
	}
?>

Re: explode() not working

Posted: Sat Nov 12, 2011 5:34 am
by twinedev
Doing your original example at the top of your post, that code worked just fine for me. You will probably want to double check what exactly is in $values['userids']. Make sure you are looking at source code, not just what is displaying in browser (for debug purposes, I usually do var_dump instead of an echo to make sure what I have).

It could possibly different characterset in data from a database? Not sure though...

-Greg

Re: explode() not working

Posted: Mon Nov 14, 2011 4:24 pm
by tangibleLime
Thanks, didn't know about var_dump.

Using var_dump, it says that the information out of the database is string(8) while the string I enter in the code is string(3). I assume this means the database is using different encoding, but nobody changed it and it was working previously. In the MySQL database, the field is marked with collation utf8_general_ci. Anything wrong there?

EDIT: After some digging I found a function to display the encoding... it says it's ASCII, which is the same as the hard-coded value. I don't see why it's having these issues of not being able to be exploded, or why it's string(8) instead of string(3).

EDIT 2: Still experimenting... when the string is three characters long, it's string(8). When the string is 5 characters long, it's string(15). When length = 7, string(22). Right off that bat I can tell that's not growing linearly because basic linear algebra will tell you there's no solution for that.

Re: explode() not working

Posted: Mon Nov 14, 2011 4:44 pm
by s.dot
My guess is your stored pipe character is actually the html entity for a pipe (& # 1 2 4 ; )

Thus, making your string 1 & # 1 2 4 ; 2 (this has a string length of 8, too)

View the source of your page, and see if you see the | or an entity code

Re: explode() not working

Posted: Mon Nov 14, 2011 4:46 pm
by tangibleLime
Oh! That's it! Good call, and thanks!