Parsing HTML Files as PHP

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

How to parse HTML in PHP?

Try to look at PHP Simple HTML DOM Parser

It has brilliant syntax similar to jQuery so you can easily select any element you want by ID or class

// include/require the simple html dom parser file

$html_string = '
<p class="Heading1-P">
<span class="Heading1-H">Chapter 1</span>
</p>
<p class="Normal-P">
<span class="Normal-H">This is chapter 1</span>
</p>
<p class="Heading1-P">
<span class="Heading1-H">Chapter 2</span>
</p>
<p class="Normal-P">
<span class="Normal-H">This is chapter 2</span>
</p>
<p class="Heading1-P">
<span class="Heading1-H">Chapter 3</span>
</p>
<p class="Normal-P">
<span class="Normal-H">This is chapter 3</span>
</p>';
$html = str_get_html($html_string);
foreach($html->find('span') as $element) {
if ($element->class === 'Heading1-H') {
$heading[] = $element->innertext;
}else if($element->class === 'Normal-H') {
$content[] = $element->innertext;
}
}

how to parse php file inside of html file

Create .htaccess file in your project directory and use below code:

RewriteEngine On
RewriteRule index.html index.php

replace index with your script file name

How to parse PHP syntax in a .html file on server?

I worked around the problem by adding a config variable which is then used in a string replacement call to replace an export-generated string with one which someone can update in a config file, before burning to CD. I can get away with this because the string replacement is essentially the only PHP in these HTML files. I'll close this question after... leaving for anyone to check up on/read about quickly.

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