Re: including an HTML file

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Well, fread reads the entire file, and we need to check the lines to
see if they contain our tag, and so that is why i used fgets.  fread
also puts the entire file into RAM, and that can cause memory problems
with large files.  But, I forgot a few pieces of my function, so I'm
adding a better version.

<?php

	$filename = 'ourfile.html';
	$tag = '<BODY>';
	$closing_tag = '</BODY>';

function insert_html($filename, $tag, $closing_tag) {	
	//Open the HTML file
	$handle = fopen($filename, 'r');
	
	$i = 0;
	//As long as the file reader isn't at the end of the file, keep looping through
	while (!feof($handle)) {
		//Read through our file in 4MB chunks, or by line, whichever comes first...
		$buffer = fgets($handle, 4096);
		//Search our buffer for the tag.  If it is there, we tell the rest
of the program to start saving out the text...
		if (strstr($buffer, $tag) == $tag) {
			$save = true;
		}
		if ($save == true) {
			$file[$i] = $buffer;
			$i++;
		}
		if (strstr($buffer, $closing_tag) == $closing_tag) {
			$save = false;
		}
	}
	//Put the data all together, and send it back...
	while (current($file)) {
		$data .= '
		' . current($file);
	}
	return $data;
}

?>

I forgot to add the part that tells it to check for when to stop
saving.  This should suit your needs Gaetono, let me know if it
doesn't work right!


-- 
The Disguised Jedi
disguised.jedi@xxxxxxxxx

PHP rocks!
"Knowledge is Power.  Power Corrupts.  Go to school, become evil"

Disclaimer: Any disclaimer attached to this message may be ignored.
This message is Certified Virus Free

-- 
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Database Programming]     [PHP Install]     [Kernel Newbies]     [Yosemite Forum]     [PHP Books]

  Powered by Linux