Process HTML Files Like PHP

Process HTML files like PHP

You need to add

AddHandler application/x-httpd-php .html .htm

How to include html files like include in php?

You can use jQuery:

HTML

<div id="container"></div>

jQuery

$("#container").load('somepage.html');

Learn more about the load function and what more you can do with it in the documentation.

Reading HTML file in PHP

Like so - to read the file?

<?php
$text= file_get_contents('yourfile.htm');
echo $text;
?>

Once you have it grabbed, you can parse the HTML - see here:
PHP Parse HTML code

Parse txt files and turn them into static html files

You could get the lines from the file one by one, instead of getting the whole file, and then formatting them individually and putting them into variables ready to be echoed out on your page. However, the method proposed by Dontfeedthecode is so far superior and more efficient that I have withdrawn the original and hope that he will approve of what I have done with his idea.

     <?php         
$files = glob("*.txt"); // Scan directory for .txt files

// Check that there are .txt files in directory
if ($files !== false) {
$numberOfFiles = count($files); // Count number of .txt files in directory

// Check if number of files is greater than one
if ($numberOfFiles > 1) {
// Advanced loop will go here to process multiple txt files
} else {

$text_array = array();
$file_handle = fopen ($files[0], "r"); // Open file
$text_array = stream_get_contents($file_handle);
$text_array = explode("\n", $text_array);
// get the top three lines
$page_title = trim($text_array[0]);
$all_lines = '<p>' . trim($text_array[0]) . ' - ' . trim($text_array[1]) . ' - ' . trim($text_array[2]) . '</p>';
// delete the top four array elements
$text_array[0] = $text_array[1] = $text_array[2] = $text_array[3] = '';
// get the remaining text
$text_block = trim(implode($text_array));
fclose ($file_handle); // Close file connection
} // endifs for first if(... statements
}
?>

HTML Output:

         <!DOCTYPE html>
<html>
<head>
<title><?php echo $page_title; ?></title>
</head>
<body>
<?php echo $all_lines . "\n" . '<p>' . $text_block .'</p>'. "\n"; ?>
</body>
</html>

A variable ready to print to file:

<?php
$print_to_file = '<!DOCTYPE html>
<html>
<head>
<title>' . $page_title . '</title>
</head>
<body>' . "\n" . $all_lines . "\n" . '<p>' . $text_block .'</p>'. "\n" .
' </body>
</html>';

echo $print_to_file;
?>

HTML looks a bit displaced in the variable here but comes out right when printed.

And finally, a version which puts a <p> tag for each line of the text.

     <?php
$files = glob("*.txt"); // Scan directory for .txt files

// Check that there are .txt files in directory
if ($files !== false) {
$numberOfFiles = count($files); // Count number of .txt files in directory

// Check if number of files is greater than one
if ($numberOfFiles > 1) {
// Advanced loop will go here to process multiple txt files
} else {

$text_array = array();
$file_handle = fopen ($files[0], "r"); // Open file

$text = stream_get_contents($file_handle);

// get the top three lines
$text_array = explode("\n", $text);
$page_title = trim($text_array[0]);
$all_lines = '<p>' . $text_array[0] . ' - ' . $text_array[1] . ' - ' . $text_array[2] . '</p>';
// set up something to split the lines by and add the <p> tags
$text_array = str_replace("\n","</p>\nxxx<p>", $text);
$text_array = explode("xxx", $text_array);

// delete the top four array elements
$text_array[0] = $text_array[1] = $text_array[2] = $text_array[3] = '';
// get the remaining text

$text_block = trim(implode($text_array));

}
}
?>

This version can use the same html/php blocks as above



Related Topics



Leave a reply



Submit