Parsing HTML With Muiltiple Foreach Loops

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • btpoole
    New Member
    • Jan 2014
    • 3

    Parsing HTML With Muiltiple Foreach Loops

    I am attempting to parse an html page but running into problems. The purpose of the parse is to create a program guide for tv programs. I am able to parse each channel, each program, program time and program description but can't seem to get it all together. Meaning I can't get the correct channel with only that channel's shows, time and description into a single array. Here is the php. I will have to post the html example separate, as I keep getting flagged when trying to post it with this post.
    Code:
    $images= $dom->getElementsByTagName('img');
    foreach($images as $img){   
    	   	$icon=   $img ->getAttribute('src'); 
          	if( preg_match('/\.(jpg|jpeg|gif)(?:[\?\#].*)?$/i', $icon) ) { //only matching types 
         			$channel[]=$img->getAttribute('alt'); 
          		}
          		}
          		foreach ($childprogram as $programname) 
            			 { 
             			$showname[]=$programname->nodeValue;  
             			 }
             			 	foreach($childtime as $programtime){
             			 		$showtime[]=$programtime->nodeValue;
             			 	}
             			 	foreach($childdescrip as $programdescrip){
             			 		    $showdescrip[]=$programdescrip->nodeValue;
             			 		    }
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    it would help to know what the source document looks like.

    anyways, the code you have shown is insufficient to follow the problem you describe. there are 4 subsequent foreach loops of which only the first one can be understood. from these 4 loops you create 4 separate variables. so I assume that would naturally count as "can't [seem to] get it all together".

    it would also help if we knew what the final result should look like.

    Comment

    • btpoole
      New Member
      • Jan 2014
      • 3

      #3
      Thank you for viewing my questions. In general what I need to do is parse html code extracting a channel name and all programs and times that occur for that channel. The end result would be written to an xml file name by the channel name (ie abc.xml). In each xml there would be the programs and times for that channel.
      Now, I can do all the xml code, but I cant figure out how to get the programs for each channel. As you can see from the code each xpath query a different type tag, so there is nothing really connecting the channel with the show or time. There has to be a separate foreach unless an xpath can built created by putting all together.
      Attached Files
      Last edited by btpoole; May 29 '15, 10:42 AM. Reason: Attaching pdf example of html

      Comment

      Working...