explode - find the number of parts

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
JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

explode - find the number of parts

Post by JKM »

I'm having a mysql field that could be like:
<----------
1,2
1

3,5,6,2323,54,233
45,66
---------->
I'm using $explode = explode("," $fetch['field']); and if($result != 0) { // make a list like this:
<a href="index.php?id=45">45</a>
<a href="index.php?id=66">66</a>
} else {
echo 'Result: 0';
}
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: explode - find the number of parts

Post by markusn00b »

JKM wrote:I'm having a mysql field that could be like:
<----------
1,2
1

3,5,6,2323,54,233
45,66
---------->
I'm using $explode = explode("," $fetch['field']); and if($result != 0) { // make a list like this:
<a href="index.php?id=45">45</a>
<a href="index.php?id=66">66</a>
} else {
echo 'Result: 0';
}
What is your question, JKM? Pardon my ignorance.

explode() returns an array of elements that were split according to the delimiter. Just like with any other array, you can find how many elements one has using the count() function.

Hope this helps,
Mark.
JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

Re: explode - find the number of parts

Post by JKM »

Sorry for not writing my question in the main text element (I wrote it in the subject).

Lets say that count($explode); returns 3, how can I list the three results?
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: explode - find the number of parts

Post by markusn00b »

JKM wrote:Sorry for not writing my question in the main text element (I wrote it in the subject).

Lets say that count($explode); returns 3, how can I list the three results?
You generally loop over the array.

Code: Select all

 
$str = "my, delimited, text";
$arr_str = explode(", ", $str);
foreach ($arr_str as $key => $node) {
    printf("%d = %s\n", $key, $node);
}
 
Something like that.

For debugging purposes, you might use the print_r() function to display an array's contents.

Other resources: Hope this helps,
Mark.
Post Reply