Reading an html file is just like reading any other file. You could use fopen() combined with fread() orfile_get_contents() or file() function. Here are some examples :
1. Using fopen() and fread()
$myFile = 'myhtmlfile.html';
// make sure the file is successfully opened before doing anything else
if ($fp = fopen($myFile, 'r')) {
$content = '';
// keep reading until there's nothing left
while ($line = fread($fp, 1024)) {
$content .= $line;
}
// do something with the content here
// ...
} else {
// an error occured when trying to open the specified file
// do something here ... ?
}
2. Using file_get_contents()
$myFile = 'yourHtmlFile.htm';
$content = file_get_contents($myFile);
if ($content !== false) {
// do something with the content
} else {
// an error happened
}
3. Using file()
Note that these function is different from the other two. This one will read the file and return an array containing each line from that file. It's very useful when you want to process a file one line at a time
$myFile = 'yourHtmlFile.htm';
$content = file($myFile);
// how many lines in this file
$numLines = count($content);
// process each line
for ($i = 0; $i < $numLines; $i++) {
// use trim to remove the carriage return and/or line feed character
// at the end of line
$line = trim($content[$i])
// do something with $line here ...
}
The story will be different when this html file is located somewhere over the internet instead of on your own computer. If this is your case then you will need to read this : reading remote file using php
0 comments:
Post a Comment