mcloan Posted November 22, 2006 Share Posted November 22, 2006 I am trying to return some retail store loacations off a site from a store locator. Each url I visit has multiple stores listed and I want to return each one. Each store is in the following format on the page and I want to return name and address to a seperate variable.nameaddressSo essenitally what I am doing is using $Contents = file_get_contents($URL); to return the information and then looping line by line. I have sucessfully returned the first line "name" no problem, but I am having trouble returning address. As you will see in the below code the name field is easily identified by using strstr($Line, 'appro')), but the address line is not easily indentified. What I was hoping to do with the address line is to use a counter that starts when the progam find a match for the name line. Then when the counter counts each loop for the next four line (there is html on 4 lines after the name and the 5 line is the address line) I would know when the counter is 5 it will be on the address line and I can set this variable.Hopefully that makes sense. In a nutshell it is not working at all. I think it may be the way the for each loop is working. Can anyone offer some input? Your help is much appreciated.Here is the code I thought would work:My logic here is once I get the contents I set a counter. The counter would become 1 when it finds the first name on the page then the for each loop loops for four more line and the counter become 5 in which case I know to retrun address on that line.[code]<?php$URL = "http://www.mysite.com/92880/"; $Contents = file_get_contents($URL); $Lines = preg_split("/\n/",$Contents); $counter=0;$strTag="False";foreach($Lines as $Line) { if (strstr($Line, 'appro')) // Found Name Match and set $strTag="True"; (this is working fine) { $strTag="True"; $dealername=$Line; echo $dealername; } If ($strTag="True") // If found a name match for each loop after this line it increases counter by 1 { $counter=$counter+1; } if ($counter=5) // If counter is 5 set address variable since this should be the address line. (this part is not working) { $address=$Line; echo $address; $strTag="False"; $counter=0; }} ?>[/code] Link to comment https://forums.phpfreaks.com/topic/28114-php-get-contents-and-for-each-loop-line-by-line/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.