Location: PHPKode > scripts > HTML2php > html2php/html2php.php
<?php
/*
** History: I saw programs in perl/php to convert html to php
** and wrote this class based on that. You can find those on
** search engines like google, yahoo.
** Usage:
require_once("html2php.php");
$h2p = new html2php;
//print "<h1>cvtflg = ". $cvtflg . "  </h1>";
if ($cvtflg == 1) // cvtflg is set in start()
{
	$h2p->file_to_change = $file_to_change;
	$h2p->convert();
}
else
	$h2p->start();
*/

class html2php 
{
	var $file_to_change;

function start()
{
	print ('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
	<html> 
	<head>
	<title>HTML to PHP</title> 
	</head> 

	<body>
	<form action="?cvtflg=1" method="post" enctype="multipart/form-data">
	Select the HTML file to convert to PHP: <br><br>
	<input type="file" name="file_to_change" size="60"> <br><br>
	<input type="submit" value="submit"> 
	<h1>After doing the conversion, you should save the file as a TEXT file (php file like filename.php)</h1>
	</form> 
	</body> 
	</html>');
}

function convert()
{
	print("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n");
	print("\n");
	print("<html>\n");
	print("<head>\n");
	print("<title>Untitled</title>\n");
	print("</head>\n");
	print("\n");
	print("<body>\n");
	print("&lt;?php\n\n<br><br>");

	// create array from file with each line an array element
	$file_array = file($this->file_to_change);

	for ($index = 0; $index < count($file_array); $index++)
	{
		$line = $file_array[$index]; 

		//insert forward slash before quotes
		$line_coded = ereg_replace("\x22", "\x5c\x22", $line);

		$line_coded = ereg_replace("\x0d", "", $line_coded); //strip carriage returns
		$line_coded = ereg_replace("\x0a", "", $line_coded); //strip line feeds

		//change < and > to character entity name for output to HTML page
		$line_coded = ereg_replace("<", "&lt;", $line_coded);
		$line_coded = ereg_replace(">", "&gt;", $line_coded); 

		//PHP line of code
		$print_me = "print(\"$line_coded\\n\");\n<br>";
		print("$print_me");
	}
	print ("<br><br>php?>");
	print("</body>\n");
	print("</html>\n");
}
}
php?>
Return current item: HTML2php