Output file of a while loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Techapprentice87
Forum Newbie
Posts: 3
Joined: Mon Mar 05, 2012 9:39 am

Output file of a while loop

Post 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: *********************************


User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Output file of a while loop

Post 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?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Output file of a while loop

Post 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.
(#10850)
Post Reply