Only from specific table tag

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
angelena
Forum Commoner
Posts: 53
Joined: Mon Nov 22, 2004 4:10 am

Only from specific table tag

Post 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]
sensey
Forum Newbie
Posts: 2
Joined: Thu Jul 27, 2006 10:53 pm

Post 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
Post Reply