Monday, 15 April 2013

parsing html using PHP DOMDocument -



parsing html using PHP DOMDocument -

i trying pull specfic section of remote html... here code...

<div id="content"> <div class="main-wide"> <ul id="nav-sub"> <li id="sub-list"><a href="/events/" class="on">list view</a></li> <li id="sub-cal"><a href="/events/calendar/">calendar view</a></li> </ul> <h2 id="ev-201302">february 2013 <a href="/events/calendar/02/2013" title="events calendar feb 2013" class="cal">(calendar view)</a></h2> <ul class="lst lst-lg"> <li> <h3><a href="http://site.com/link_1>link text one</a></h3> <ul class="meta"> <li>february 1st - feb 2nd, 2013</li> </ul> </li> <li> <h3><a href="http://site.com/link_2>link text two</a></h3> <ul class="meta"> <li>february 1st - feb 28th, 2013</li> </ul> </li> <li> <li> <h3><a href="http://site.com/link_3>link text three</a></h3> <ul class="meta"> <li>february 1st - feb 15th, 2013</li> </ul> </li> </ul> </div> </div>

i want grab between <ul class='lst lst-lg'> , create can echo want following....

<tr> <td align='left'>february 1st - feb 2nd, 2013</td> <td align='left'><a href='http://site.com/link_1'>link text one</a></td> </tr> <tr> <td align='left'>february 1st - feb 28th, 2013</td> <td align='left'><a href='http://site.com/link_2'>link text two</a></td> </tr> <tr> <td align='left'>february 1st - feb 15th, 2013</td> <td align='left'><a href='http://site.com/link_3'>link text three</a></td> </tr>

and on that... far have this...

function get_data($url) { $ch = curl_init(); $timeout = 5; curl_setopt($ch, curlopt_url, $url); curl_setopt($ch, curlopt_returntransfer, 1); curl_setopt($ch, curlopt_connecttimeout, $timeout); $data = curl_exec($ch); curl_close($ch); homecoming $data; } $page = get_data('http://site.com/index.php'); $doc = new domdocument(); $dom->preservewhitespace = false; $doc->loadhtml($page); $uls = $doc->getelementsbytagname('ul'); $i = 0; while($table = $uls->item($i++)){ $class_node = $table->attributes->getnameditem('class'); $li_node = $table->nodename; if($class_node){ echo $table->nodename . " - " . $table->nodevalue . "<br>"; } }

so far have been trying echo out values stuff within while loop more playing , trying learn. have been able info page formatting , getting right info problem @ point.

thanks much in advance!

php domdocument

No comments:

Post a Comment