Page 1 of 1

Only from specific table tag

Posted: Mon May 01, 2006 11:43 pm
by angelena
Dear All,

I have a code such as below.My intention was to grab only data within a table tag from the respective html,but unable to do so as inside the html ,there's many table tag and no ID is given to each tag.
Can anyone please advice me on how i able to retrieve the data within the table tag that i wanted to grab?

Code: Select all

$ch = curl_init("http://localhost/test.htm");
curl_setopt($ch, CURLOPT_USERAGENT, "Internet Explorer");
ob_start();
curl_exec($ch);
curl_close($ch);
$str = ob_get_contents();
ob_end_clean();

preg_match("/\<table>(.*?)\<\/table\>/is", $str, $byname);

preg_match_all("/\<tr.*?>(.*?)\<\/tr\>.*?\<A.*?\>(.*?)\<BR\>/is", $byname[0], $moviedata);

$movies = array();

for($i=0; $i<count($moviedata[1]); $i++)
{
 $score=$moviedata[1][$i];
 $title=$moviedata[2][$i];
 $title=preg_replace("/<.*?>/","",$title);
 $title=html_entity_decode($title);
 $movies[]=array($score,$title);
 }

Code: Select all

foreach($movies as $movie)  
{  
echo($movie[1]);  echo($movie[0]);  
}
[php][/php]

Posted: Thu Aug 03, 2006 10:50 pm
by sensey
//this function return the position of regex

Code: Select all

function preg_pos($sPattern, $sSubject, &$FoundString, $iOffset = 0) {
     $FoundString = NULL;
    
     if (preg_match($sPattern, $sSubject, $aMatches, PREG_OFFSET_CAPTURE, $iOffset) > 0) {
       $FoundString = $aMatches[0][0];
       return $aMatches[0][1];
     }
     else {
       return FALSE;
     }
}
whith this u can get the position of ur desired text, u need add some substr to the founstring and the job its done :D.
im doing some similar, i just need work a little more with regex