Results 1 to 3 of 3

Thread: Explode Problem

  1. #1
    Join Date
    Apr 2012
    Location
    Central New Jersey
    Posts
    286
    Thanks
    95
    Thanked 3 Times in 3 Posts

    Default Explode Problem

    $bigData: 210210083045*68.197.65.57*10*2****210210083303*107.77.225.115*9*1****

    The code:
    PHP Code:
    $lineItem explode ("*"$bigData);                                    // create array
    foreach ($lineItem as $item) {
        echo 
    "Item = " $item "<br />"
    The result:

    Item = 210210083045
    Item = 68.197.65.57
    Item = 10
    Item = 2
    Item =
    Item =
    Item =
    Item = 210210083303
    Item = 107.77.225.115
    Item = 9
    Item = 1
    Item =
    Item =
    Item =
    Item =

    The problem: The last result set when I run this script has four blank Items. Every other result set has three. Just trying to figure out why the last result set is treated differently.

    https://www.marainlaw.com/page.php?here=reviewActivity shows inputs and outputs.

    Knowing that the script is doing what it's doing, I've developed a workaround, so the added blank Item on the last set is not holding me back. So now it's just a matter of curiosity.

    Thank you all for your patience with my recent spate of questions.
    Last edited by marain; 02-11-2021 at 02:39 AM. Reason: Discription of what's happening misstated; provide additional information;

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,413
    Thanks
    101
    Thanked 115 Times in 113 Posts

    Default

    Let's simplify this a bit.

    Code:
    $lineItem = explode ("*", $bigData);
    divides the array based on "*".

    In the case of $bigData="a*b"; you have:

    Code:
    $lineItem[0]=="a";
    $lineItem[1]=="b";
    In the case of $bigData="a**b"; you have:

    Code:
    $lineItem[0]=="a";
    $lineItem[1]=="";
    $lineItem[2]=="b";
    The explode function first takes "a" from "a**b" creating $lineItem[0]=="a"; and leaving "*b" because the first * was used as a divider and removed as well.
    explode now takes "*b" and divides it into two values. That part to the left of "*" is nothing so the second value created is $lineItem[1]==""; and the last part to the right is "b" which is $lineItem[2]=="a";.

    In the case of "a**"

    you will have:

    Code:
    $lineItem[0]=="a";
    $lineItem[1]=="";
    $lineItem[2]=="";
    a = the first piece.
    ""= the part between "a" and the first "*".
    ""= the part between the second "*" and the nothing to the right of the second "*".
    To choose the lesser of two evils is still to choose evil. My personal site

  3. The Following User Says Thank You to james438 For This Useful Post:

    marain (02-11-2021)

  4. #3
    Join Date
    Apr 2012
    Location
    Central New Jersey
    Posts
    286
    Thanks
    95
    Thanked 3 Times in 3 Posts

    Default

    James,

    Your explanation comports completely with my understanding. The final set, however, seemed to have been acting as if there were an additional separator. I developed a workaround to that, removed the input data from the affected web page display, and marked the thread "resolved" (just now) because the workaround was sufficient.

Similar Threads

  1. explode
    By james438 in forum PHP
    Replies: 9
    Last Post: 03-22-2012, 06:05 AM
  2. Explode Function Help
    By DeSaaD37 in forum PHP
    Replies: 4
    Last Post: 03-16-2009, 04:59 PM
  3. Using file_get_contents() and explode()
    By alexjewell in forum PHP
    Replies: 3
    Last Post: 01-13-2007, 03:17 AM
  4. php explode question
    By centenial in forum PHP
    Replies: 3
    Last Post: 10-10-2006, 09:43 PM
  5. explode.js
    By chloedearest in forum JavaScript
    Replies: 7
    Last Post: 08-10-2006, 06:21 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •