Jump to content

Parsing HTML


taith

Recommended Posts

say we have template.html
[code]
<!-- Body -->
<table>
<tr>
  <td></td>
</tr>
</table>
[/code]

i want somthing that can load that file, then alter it(in realtime) to input variables
[code]
<!-- Body -->
<table>
<tr>
  <td><?=$var?></td>
</tr>
</table>
[/code]

any suggestions?
Link to comment
Share on other sites

place a marker variable or something like hte word body
then do a string_replace
or whatever on it, that is what I had to do to create dynamic php on a file, I had to get the file, copy it, and edit it, ehre is the code if that helps.

[code]<?php
session_start();
function read_file($file)
{
  if(!function_exists("file_get_contents"))return file_get_contents($file);
  $ifile = fopen($file,"r");
  $contents = false;
  if($ifile) while (!feof($ifile)) $contents .= fgets($ifile);
  fclose($ifile);
  return $contents;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php
include './includes/meta.inc.php';
?>
</head>
<body>
<?php
include './includes/header.inc.php';
?>
<!-- Begin Content -->
<div id="topimage"><img src="images/topimage.gif" alt="Face Image" /></div>
<div id="rightcontent">
<?php
if ($_SESSION['controller'] == true) {
mysql_connect("localhost", other);
mysql_select_db("hasbadse_hbservice");
// create user account.
$errorhandler = "";
$insert = "INSERT INTO userinfo SET username = '$_SESSION[username]', password = '$_SESSION[password]', email = '$_SESSION[email]', subdomainname = '$_SESSION[subdomain]';";
mysql_query($insert);
$select = "SELECT * FROM userinfo WHERE username = '$_SESSION[username]';";
$query = mysql_query($select);
if ($row = mysql_fetch_array($query)) { // retrieve userid with user account.
$id = $row['id'];
} // end getting userid.


$header = $_SESSION['header'];
$text = $_SESSION['text'];
$insertcms = "INSERT INTO usercms (text, userid) VALUES ('$text', '$id');";
if (!mysql_query($insertcms)) {
$errorhandler .= "There was a problem inserting the Content Management Information.<br />";
$errorhandler .= "Please contact support about this problem.<br />";
}
// VERY IMPORTANT, everything below is used for directory creation.
$var = preg_replace('/[^\x09\x0A\x0D\x20-\x7F]/e', '"&#".ord($0).";"', $_SESSION['subdomain']);
$dir = "userpages/$var";
mkdir("$dir", 0700);
if (!mkdir) {
$errorhandler .= "unable to create the directory. Please contact support or try again.<br />";
}

$buffer = read_file($_SESSION['template']);
$buffer = str_replace("hold", $id, $buffer);
// here I can do all my changes to the buffer variable(which contains the file information)
$outfile = fopen("userpages/{$var}/index.php", "w"); // open writeable
fwrite($outfile, $buffer);
fclose ($outfile);
chmod("userpages/{$var}", 0755);
chmod("userpages/{$var}/index.php", 0755);

// VERY IMPORTANT, everything above here is used for directory creation.
$insertdn = "INSERT INTO subdomain (path, subdomainname, userid) VALUES ('$dir', '$_SESSION[subdomain]', '$id');";
if (!mysql_query($insertdn)) {
$errorhandler .= "There was a problem, please contact support immediately.<br />";
}
// start check for error handler
if ($errorhandler != "") {
echo "<span style=\"color:red;\">";
echo $errorhandler;
echo "</span>";
}
if ($errorhandler == "") {
?>
<h4>Thank You</h4>
<p>Thank you for your purchase, you can go over to the administration page, and login there.</p>
<a href="/useradmin/useradmin.php" title="User Admin">User Admin</a>
<?php
}
}else {
echo "You have to go through the entire signup process, before coming to this page, in order <br />";
echo "for your account to be able to be created.<br />";
}
?>
<?php
include './includes/footer.inc.php';
?>
</div>
</body>
</html>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Thank You</title>
</head>
<body>
</body>
</html>
[/code]
You see what I mean, now however, here are my template pages, that I used, it shows you what I did in those pages, that allowed me to do my search and replace with regular expressions

The template is chosen based on what they picked, for instance if they pick template1.php it takes it copies the information int eh file (the actual .php page itself), and saves it in another file, with the dynamic hcanges done to it.  Works pretty smoother

Template1.php
[code]<?php
session_start();
mysql_connect("localhost", other);
mysql_select_db("hasbadse_hbservice");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="/templates/template1/css/globaltemplate1.css" />
<title>Hasbadservice</title>
</head>
<body>
<div id="wrapper">
<!-- Entire Header Area -->
<div id="header">
<div id="leftheader">
<?php
$selectuser = "SELECT * FROM userinfo WHERE id = 'hold';";
$queryuser = mysql_query($selectuser);
if ($row = mysql_fetch_array($queryuser)) {
?>
<pre>
<?php
echo $row['subdomainname'] . ".\n";
?>
HasBadService.
Com
</pre>
<?php
}
?>
</div>
<img src="/templates/template1/images/star.gif" id="star" alt="Star Symbol" />
</div>
<!-- End Header Area -->
<div id="leftcolumn">
</div>
<div id="rightcolumn">
<!-- Not part of original template.  This was added in dynamically -->
<div class="content">
<?php
$select = "SELECT * FROM usercms WHERE userid = 'hold';";
$query = mysql_query($select);
while ($row = mysql_fetch_array($query)) {
echo "<h4>" . stripslashes($row['header']) . "</h4>";
echo "<p>" . stripslashes($row['text']) . "</p>";
}
?>
</div>
<!-- End section that was done dynamically -->
</div>
</div>
<hr />
</body>
</html>[/code]

template2.php

[code]<?php
session_start();
mysql_connect("localhost", "hasbadse_joyel", "joyel");
mysql_select_db("hasbadse_hbservice");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="/templates/template2/css/globaltemplate2.css" />
<title>Hasbadservice</title>
</head>
<body>
<div id="header">
<?php
$selectuser = "SELECT * FROM userinfo WHERE id = 'hold';";
$queryuser = mysql_query($selectuser);
if ($row = mysql_fetch_array($queryuser)) {
echo strtoupper($row['subdomainname']) . ".HASBADSERVICE.COM";
}
?>
<div id="bar">&nbsp;</div>
</div>
<div id="wrapper">
<div id="leftbox">
<div id="upper"></div>
<div id="lower"></div>
</div>
<div id="rightbox">
<!-- This section is not part of the original content, it's all dynamic -->
<div class="content">
<?php
$select = "SELECT * FROM usercms WHERE userid = 'hold';";
$query = mysql_query($select);
while ($row = mysql_fetch_array($query)) {
echo "<h4>" . stripslashes($row['header']) . "</h4>";
echo "<p>" . stripslashes($row['text']) . "</p>";
}
?>
</div>
<!-- End dynamic addition -->
</div>
</div>
<div id="footer"></div>
</body>
</html>[/code]
As you can see, it is a bad way for some things,f or instance, if the database information chagnes all the old templates are lost, the people who created accounts, that's what my new "config" file is for instead.  BUt it gives you the idea of what you wanted to do.
Link to comment
Share on other sites

here we go... for all who wanted to see...
[code]
<?
$body=test;

$handle = @fopen("test.html", "r");
if($handle){
while(!feof($handle)) $buffer[] = fgets($handle, 4096);
fclose($handle);
}

foreach($buffer as $line){
if(strstr($line, '<!--')&&strstr($line, '-->')){
  $len=strlen($line);
  $before=substr($line, 0, strpos($line,"<!--"));

  $mid = substr($line, strpos($line,"<!--")+4);
  $mid=substr($mid,0,strpos($line,"-->")-10);
  $mid=trim($mid);

  $after=substr($line,strpos($line,"-->")+3);
  echo $before.$$mid.$after;
}else echo $line;
}
?>
[/code]

then in test.html
[code]
<table>
<tr>
  <td><!-- body --></td>
</tr>
</table>
[/code]
Link to comment
Share on other sites

yes... but the whole idea here is inforcing templates...

[code]
<?
$body=test;

$required=array('body','head');

$handle = @fopen("test.html", "r");
if($handle){
while(!feof($handle)) $buffer[] = fgets($handle, 4096);
fclose($handle);
}

foreach($buffer as $line){
if(strstr($line, '<!--')&&strstr($line, '-->')){
  $len=strlen($line);
  $before=substr($line, 0, strpos($line,"<!--"));

  $mid = substr($line, strpos($line,"<!--")+4);
  $mid=substr($mid,0,strpos($line,"-->")-10);
  $mid=trim($mid);
  $i[$mid]=true;

  $after=substr($line,strpos($line,"-->")+3);
  $temp .= $before.$$mid.$after;
}$temp .= echo $line;
}
foreach($required as $k){
if(!isset($i[$k])) $failed=true;
}
if($failed){
$temp='';
require('template/default.php');
}
?>
[/code]

script isnt tested... but thats the jist...
Link to comment
Share on other sites

taith, i know what youre getting at. but its well documented that PHP itself is a perfectly good templating engine itself. the short tags + alternative syntax are more than adequate. i'm sure a few people have noticed me point them towards this article before:
http://www.massassi.com/php/articles/template_engines/

which not only explains why template engines are not all that great, but also explains how to STILL use this whole templating concept but without the bloat/overhead.

tis a good read, even if you're adament not to go down that road.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.