0

I'm looping through an array of strings in PHP. Specifically, I'm looking for double dots in each array row (..). If no dots are found, the entire row is appended to a second array row. If it is found, then I split the row, put the left half in the current (second) array row, and the right half in the next row.

This is what I have so far:

$fudgedindex = 0;
$parsedrows = array('');

foreach($refinedlist as $rowtoparse)
{
    $pos2 = strpos($rowtoparse, "..");

    //echo $refinedlist[$index] . "<br>";
    if ($pos2 === false)
    {
        $parsedrows[$fudgedindex] = $parsedrows[$fudgedindex] . $rowtoparse;
    }
    else
    {
        $length = strlen($rowtoparse);
        $partialleft = substr($rowtoparse, 0, $pos2);
        $partialright = substr($rowtoparse, $pos2);
        $parsedrows[$fudgedindex] = $parsedrows[$fudgedindex] . $partialleft;
        $fudgedindex = $fudgedindex + 1;
        $parsedrows = '';

        $parsedrows[$fudgedindex] = $parsedrows[$fudgedindex] . $partialright;
    }
}

I keep getting undefined index after I increment $fudgedindex.

It's been a while since I've been on PHP, and I get the feeling that I'm either overthinking this and being a bit too procedural, or doing a head-slap error.

Any help appreciated! And once again, thanks for taking the time to help. =)

EDIT: Per the comments, here's what I'm aiming for...

$refinedlist[0]="Today I bought milk..Then I went to the "
[1]="store and bought even more food "
[2]="such as apples..I was happy..Then"
[3]=" I went to stackoverflow.com and asked this question "
[4]="to solve my problem.."

Result should be:

$parsedrows[0]="Today I bought milk"
[1]="Then I went to the store and bought even more food such as apples"
[2]="I was happy."
[3]="Then I went to stackoverflow.com and asked this question to solve my problem"

EDIT 2: The error is gone, so the initial issue is fixed. However, when I run this after:

foreach($parsedrows as $rowtoprint)
{
    echo $rowtoprint . "<br>";
}

Only the last row shows up.

6
  • Can you give an example of 3-5 values in $refinedlist and expected outcome of $parsedrows? Commented Jul 8, 2015 at 20:46
  • mseifert already aswered your querstion. So there's time to ask the question behind your current problem: What's the source of $refinedlist? Commented Jul 8, 2015 at 20:56
  • Did your question get answered? Commented Jul 9, 2015 at 4:45
  • The initial question, yes (and thanks!). I added a second edit for the final $parsedrows error I'm getting. Commented Jul 9, 2015 at 16:43
  • See my updated answer to address your edit. It needs one more fix to handle when there are two .. in a line. Working on it. Commented Jul 9, 2015 at 18:03

1 Answer 1

1

Your problem is with

    $parsedrows[$fudgedindex] = $parsedrows[$fudgedindex] . $partialright;

You've increased $fudgedindex and are setting a new array element with this statement. Yet you are also referencing this new element (the right side of the =) which doesn't exist yet - hense the error.

You should just say

    $parsedrows[$fudgedindex] = $partialright;

since it is the beginning of the next row.

Your second problem starts with noticing you are reinitializing $parsedrows and wiping out all your previous array elements. You need to remove $parsedrows = '';

However, because you can have a line with more than one "..", you need to include a loop to process each line until it is done. I had to rewrite the whole thing to accomplish this. This produces the result you want

<?php

$refinedlist[0]="Today I bought milk..Then I went to the ";
$refinedlist[1]="store and bought even more food ";
$refinedlist[2]="such as apples..I was happy..Then";
$refinedlist[3]=" I went to stackoverflow.com and asked this question ";
$refinedlist[4]="to solve my problem..";

$fudgedindex = 0;
$parsedrows = array('');

foreach($refinedlist as $rowtoparse)   
{
    while (($pos2 = strpos($rowtoparse, "..")) !== false)
    {
        $length = strlen($rowtoparse);
        $partialleft = substr($rowtoparse, 0, $pos2);

        // keep what is not yet processed in the original var
        // while loop will process again
        $rowtoparse = substr($rowtoparse, $pos2 + 2);

        // add to current row
        $parsedrows[$fudgedindex] = $parsedrows[$fudgedindex] . $partialleft;

        // increment the row & initialize
        $fudgedindex += 1;
        $parsedrows[$fudgedindex] = "";
    }

    // add whatever is left to the current row
    $parsedrows[$fudgedindex] = $parsedrows[$fudgedindex] . $rowtoparse;
}

foreach($parsedrows as $rowtoprint)
{
    echo $rowtoprint . "<br>";
}

This produces (without the highlighting):

Today I bought milk
Then I went to the store and bought even more food such as apples
I was happy
Then I went to stackoverflow.com and asked this question to solve my problem
Sign up to request clarification or add additional context in comments.

2 Comments

Also the code only increases fudgedindex if the line contains .. after the partialleft is added, not before adding a line without ...
@OIS: That's by design. See my edits for more information.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.