5

If I have the html file:

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

How can I use PHP to get the string "Important Information" from the file?

JJJollyjim
  • 5,837
  • 19
  • 56
  • 78
  • 2
    Have a look at http://stackoverflow.com/questions/3577641/best-methods-to-parse-html – Jacob Feb 16 '11 at 04:00
  • It isn't (mostly) the parsing i'm worried about, more getting the code in the first place – JJJollyjim Feb 16 '11 at 04:05
  • How can I turn " Important Information – JJJollyjim Feb 16 '11 at 04:07
  • Title is highly misleading, you're not "getting the HTML source code." You're just getting the text. – BoltClock Feb 16 '11 at 04:08
  • No sorry, I am, how can I turn index.html into php $variable? – JJJollyjim Feb 16 '11 at 04:08
  • What do you suggest I edit the title into? – JJJollyjim Feb 16 '11 at 04:11
  • Your question asks how to extract the content between the HTML comment nodes. That's what you use a parser for. Please update your question if that's not what you want to do. – Gordon Feb 16 '11 at 08:34
  • @Gordon I actually found the initial version of the question more explanatory in what he wanted to do. He wasn't asking how to parse the file, he was asking how to actually open and read the file as a string into a single variable. – Endophage Feb 17 '11 at 21:52

3 Answers3

5

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

Endophage
  • 21,038
  • 13
  • 59
  • 90
2

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

If your HTML is in a variable you can just 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;
?>

You can see the output here.

Murilo Vasconcelos
  • 4,677
  • 24
  • 27
-1

You can fetch "HTML" by this

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

and any operation on DOM then read following docs: http://de.php.net/manual/en/book.dom.php

Manish Trivedi
  • 3,481
  • 5
  • 23
  • 29