Log in

View Full Version : Resolved Explode Problem



marain
02-10-2021, 04:06 PM
$bigData: 210210083045*68.197.65.57*10*2****210210083303*107.77.225.115*9*1****

The 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.

james438
02-11-2021, 02:25 AM
Let's simplify this a bit.


$lineItem = explode ("*", $bigData);

divides the array based on "*".

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


$lineItem[0]=="a";
$lineItem[1]=="b";

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


$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:


$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 "*".

marain
02-11-2021, 02:43 AM
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.