Page 1 of 1

Output file of a while loop

Posted: Thu Mar 08, 2012 1:14 pm
by Techapprentice87
Hi I am a new php developer that is having some issue with a part of code that I wrote. I listed my script in another forum post but I think i could be a bit more specific with the issue I am having. So i know the code finds the searchString I am looking for, and pulls it correctly the first time. The issue is that it continues to run, though I know the loop I created will pull all matching statements. Now one thing I tried was to look for a more unique code in the html site I was using, with no good feedback. If anyone could give me some advice on what I could do that be helpful.
part of the code

Code: Select all

$index = stripos($fileContents, $searchString);
										while ($index !== false) {
											if ($index >= 0) {
												//if ($debug) {
													print "endString: " . $endString . "\n";
													print "startIndex: " . ($index+strlen($searchString)) . " " . strlen($fileContents) . "\n";
												//}
												$endIndex = stripos($fileContents, $endString, $index+strlen($searchString));
												if ($debug) {
													print "endIndex: " . $endIndex . "\n";
													print " index: " . ($index+strlen($searchString)) . "\n";
													print "endIndex: " . ($endIndex - ($index+strlen($searchString))) . "\n";
												}
												$price = trim(substr($fileContents, $index+strlen($searchString), $endIndex - ($index+strlen($searchString))));

												if ($debug) {
													print "\n******** PRICE: *********************************" . $price . "\n";
												}
											}
											$dollarSignIndex = stripos($price,$endString, "\$");
											if ($i2 == 0 || $dollarSignIndex === false) {
												if (strlen($priceString) > 0) {
													$priceString .= "|";
												}
												$priceString .= $price;
											}
The output file looks something like this:

Code: Select all


<tr>
    <td class="priceBlockLabel">Issues:</td>
    <td>12

******** searchString: <td> ********************************* index: 81480
endString:  issues / 12 months</td>
startIndex: 111679 204556
endIndex: 111681
 index: 111679
endIndex: 2

******** PRICE: *********************************12

******** searchString: <td> ********************************* index: 111675
endString:  issues / 12 months</td>
startIndex: 112322 204556
endIndex: 
 index: 112322
endIndex: -112322

******** PRICE: *********************************

******** searchString: <td> ********************************* index: 112318
endString:  issues / 12 months</td>
startIndex: 115996 204556
endIndex: 
 index: 115996
endIndex: -115996

******** PRICE: *********************************



Re: Output file of a while loop

Posted: Thu Mar 08, 2012 4:05 pm
by twinedev
Ahh, this looks like something I used to write before I learned Regular Expressions! They may be confusing at first, but if you get a program like RegexBuddy, it is awesome for learning how to build them and test things.

DO you have a sample of the file you are reading data from?

Re: Output file of a while loop

Posted: Thu Mar 08, 2012 4:32 pm
by Christopher
If you want the loop to stop after the first match then either add a boolean variable to your loop condition and set it, or add a break statement.