0

I am trying to fetch the content inside a <div> via file_get_contents. What I want to do is to fetch the content from the div resultStats on google.com. My problem is (afaik) printing it.

A bit of code:

$data = file_get_contents("https://www.google.com/?gws_rd=cr&#q=" . $_GET['keyword'] . "&gws_rd=ssl");
preg_match("#<div id='resultStats'>(.*?)<\/div>#i", $data, $matches);

Simply using

print_r($matches);

only returns Array(), but I want to preg_match the number. Any help is appreciated!

Edit: thanks for showing me the right direction! I got rid of the preg_ call and went for DOM instead. Although I am pretty new to PHP and this is giving me an headache; I found this code here on Stack Overflow and I am trying to edit it to get it to work. This far I only receive a blank page, and don't know what I am doing wrong.

$str = file_get_contents("https://www.google.com/search?source=hp&q=" . $_GET['keyword'] . "&gws_rd=ssl");
$DOM = new DOMDocument;   
@$dom->loadHTML($str);

//get
   $items = $DOM->getElementsByTagName('resultStats');

//print
   for ($i = 0; $i < $items->length; $i++)
        echo $items->item($i)->nodeValue . "<br/>";

} else { exit("No keyword!") ;}
halfer
  • 19,824
  • 17
  • 99
  • 186
Mikael W
  • 1
  • 1
  • 2
    OMG. preg_match on HTML!!!! You are getting [here](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) – bansi Jul 27 '14 at 19:20
  • 1
    http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php – Jason OOO Jul 27 '14 at 19:24
  • @jason-ooo Thanks for showing me the right direction! I got rid of the preg_ and went for DOM instead. Although; I am pretty new to PHP and this is giving me an headache... I found this code here on StackOverflow and I am trying to edit it to get it to work. This far i only receive a blank page, and dont know what I am doing wrong. Pastebin: http://pastebin.com/Y4GpT961 – Mikael W Jul 27 '14 at 19:34
  • Please bear in mind that questions should be formatted to be useful to a long term readership - thus edit markers, and answers in questions, is not ideal. I'll try to fix it up. – halfer Jul 27 '14 at 21:29
  • I've moved your answer to a separate answer block. I converted your pasteboard to a code block - we don't like those too much as they can be brittle in practice. Thanks for posting your answer. – halfer Jul 27 '14 at 21:38

1 Answers1

0

Posted on behalf of the OP.

I decided to use the PHP Simple HTML DOM Parser and ended up something like this:

include_once('/simple_html_dom.php');
$setDomain = "https://www.google.com/search?source=hp&q=" . $_GET['keyword'] . "&gws_rd=ssl";
$str = file_get_html($setDomain);
$html = str_get_html($str);
$html->find('div div[id=resultStats]', 0)->innertext . '<br>';

Problem solved!

halfer
  • 19,824
  • 17
  • 99
  • 186