Padmanathan J
Padmanathan J

Reputation: 4620

Preg_match not working in inner table?

I want to scrap one website table using curl and preg_match

My url is http://hosts-file.net/?s=Browse&f=EMD

my curl

$url = 'http://hosts-file.net/?s=Browse&f=EMD';     


        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") ); 
        curl_setopt($ch, CURLOPT_NOBODY, false);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $body= curl_exec ($ch);
        curl_close ($ch);

i need to scrap one table.

my preg_match function is given below

preg_match_all('/<table class=\"main_normal(.*?)\">(.*?)<\/table>/s',$body,$vv,PREG_SET_ORDER);

But it returns empty array only

Please guide me

Upvotes: 0

Views: 234

Answers (1)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89557

An example with DOMDocument and DOMXPath:

$doc = new DOMDocument();
@$doc->loadHTML($body);
$xpath = new DOMXPath($doc);
$links = $xpath->query('/html/body/table/tr/td/table/tr/td/table[@class="main_normal"]/tr/td[2]/a[1]/text()');
foreach($links as $link) {
    echo $link->nodeValue."<br/>"; }

You can replace the fourth line with a relative path, but it is less efficient:

$links = $xpath->query('//table[@class="main_normal"]/tr/td[2]/a[1]/text()');

Upvotes: 1

Related Questions