explode() not working

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
tangibleLime
Forum Newbie
Posts: 3
Joined: Sat Nov 12, 2011 12:03 am

explode() not working

Post 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.
mikeashfield
Forum Contributor
Posts: 159
Joined: Sat Oct 22, 2011 10:50 am

Re: explode() not working

Post 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";
	}
?>
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: explode() not working

Post 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
tangibleLime
Forum Newbie
Posts: 3
Joined: Sat Nov 12, 2011 12:03 am

Re: explode() not working

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: explode() not working

Post 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
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
tangibleLime
Forum Newbie
Posts: 3
Joined: Sat Nov 12, 2011 12:03 am

Re: explode() not working

Post by tangibleLime »

Oh! That's it! Good call, and thanks!
Post Reply