1

I'm getting the source code of a page in a variable $html with this line:

$html = file_get_contents('http://www.google.com');

And when I do <textarea><?php echo htmlentities($html); ?></textarea>

It works awesome.

Now, suppose, I want to take out every <h1> tag in the page with its content in a variable $h1, how do I do this from $html variable?

mehulmpt
  • 15,861
  • 12
  • 48
  • 88

1 Answers1

-1

You can use Simple Html Dom Parser

Download the required file and try following code:

<?php

require_once ('simple_html_dom.php');
$html = file_get_contents('http://www.google.com');
$domHtml = str_get_html($html);
foreach ($domHtml->find('h1') as $element) {
    echo '<h1>' . $element->innertext . '</h1>';
}
Andy Librian
  • 911
  • 5
  • 12