Get HTML source code of a page with PHP

If I have an html file:

<!doctype html>
 <html>
  <head></head>
   <body>
    <!-- Begin -->
    Important Information
    <!-- End -->
   </body>
  </head>
 </html>

How can I use PHP to get the “Important Information” line from a file?

+5
source share
3 answers

If you've already parsed the parsing, just use file_get_contents(). You can pass it the url and it will return the content found at the url, in this case, html. Or, if you have a file locally, you pass it the path to the file.

+5
source

In this simple example, you can open the file and do fgets()it until you find the line with <!-- Begin -->and save the lines until you find it <!-- End -->.

If your HTML is in a variable you can simply do:

<?php
$begin = strpos($var, '<!-- Begin -->') + strlen('<!-- Begin -->'); // Can hardcode this with 14 (the length of your 'needle'
$end   = strpos($var, '<!-- End -->');

$text = substr($var, $begin, ($end - $begin));

echo $text;
?>

.

+2

"HTML"

//file_get_html function from third party library
// Create DOM from URL or file
$html = file_get_html('http://www.example.com/');

DOM, : http://de.php.net/manual/en/book.dom.php

-1
source

All Articles