php array woes when parsing a string -
i'm looping through array of strings in php. specifically, i'm looking double dots in each array row (..). if no dots found, entire row appended second array row. if found, split row, put left half in current (second) array row, , right half in next row.
this have far:
$fudgedindex = 0; $parsedrows = array(''); foreach($refinedlist $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 increment $fudgedindex.
it's been while since i've been on php, , feeling i'm either overthinking , being bit procedural, or doing head-slap error.
any appreciated! , once again, taking time help. =)
edit: per comments, here's i'm aiming for...
$refinedlist[0]="today bought milk..then went " [1]="store , bought more food " [2]="such apples..i happy..then" [3]=" went stackoverflow.com , asked question " [4]="to solve problem.."
result should be:
$parsedrows[0]="today bought milk" [1]="then went store , bought more food such apples" [2]="i happy." [3]="then went stackoverflow.com , asked question solve problem"
edit 2: error gone, initial issue fixed. however, when run after:
foreach($parsedrows $rowtoprint) { echo $rowtoprint . "<br>"; }
only last row shows up.
your problem with
$parsedrows[$fudgedindex] = $parsedrows[$fudgedindex] . $partialright;
you've increased $fudgedindex
, setting new array element statement. yet referencing new element (the right side of =) doesn't exist yet - hense error.
you should say
$parsedrows[$fudgedindex] = $partialright;
since beginning of next row.
your second problem starts noticing reinitializing $parsedrows , wiping out previous array elements. need remove $parsedrows = '';
however, because can have line more 1 "..", need include loop process each line until done. had rewrite whole thing accomplish this. produces result want
<?php $refinedlist[0]="today bought milk..then went "; $refinedlist[1]="store , bought more food "; $refinedlist[2]="such apples..i happy..then"; $refinedlist[3]=" went stackoverflow.com , asked question "; $refinedlist[4]="to solve problem.."; $fudgedindex = 0; $parsedrows = array(''); foreach($refinedlist $rowtoparse) { while (($pos2 = strpos($rowtoparse, "..")) !== false) { $length = strlen($rowtoparse); $partialleft = substr($rowtoparse, 0, $pos2); // keep not yet processed in original var // while loop process again $rowtoparse = substr($rowtoparse, $pos2 + 2); // add current row $parsedrows[$fudgedindex] = $parsedrows[$fudgedindex] . $partialleft; // increment row & initialize $fudgedindex += 1; $parsedrows[$fudgedindex] = ""; } // add whatever left current row $parsedrows[$fudgedindex] = $parsedrows[$fudgedindex] . $rowtoparse; } foreach($parsedrows $rowtoprint) { echo $rowtoprint . "<br>"; }
this produces (without highlighting):
today bought milk went store , bought more food such apples happy went stackoverflow.com , asked question solve problem
Comments
Post a Comment