Page 1 of 1

Reading HTML file containing PHP variable into a string

Posted: Wed Jan 07, 2004 9:03 am
by igoy
This is a scenario....

I have a HTML file, which contains PHP variables, or rather say I have HTML file which I want to include and insert PHP variable in it.

Okay, let me get it straight......

I need to send a Mail (HTML mail), for that I have made a HTML file which is format in which mail need to be send, previously I use to copy the whole HTML code in a string. But when I need to make changes in format It use to be a big PITA, so instead now what I want to do is use an external HTML file.

The thing is some of the part of this HTML file is need to be dynamic, PHP variable which are posed by form.

Somehow using following function I could get content of HTML file in string, but PHP vaible copies as normal HTML text.

check this out...

Code: Select all

<?php // File_Get_Content Function

function file_get_contents($f) {
   ob_start();
   $retval = @readfile($f);
   if (false !== $retval) { // no readfile error
       $retval = ob_get_contents();
   }
   ob_end_clean();
   return $retval;
}


?>
My HTML file is like this

Code: Select all

&lt;html&gt;
&lt;body&gt;
&lt;table width="500" border="0" align="center" cellpadding="5" cellspacing="0"&gt;
  &lt;tr&gt;
    &lt;td height="30" colspan="3" class="headText"&gt;Sender's Information&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td width="100" height="20" class="genText"&gt;&lt;strong&gt;Name&lt;/strong&gt;&lt;/td&gt;
    &lt;td width="30" class="genText"&gt;&lt;strong&gt;:&lt;/strong&gt;&lt;/td&gt;
    &lt;td width="370" class="genText"&gt;$sender&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td height="20" class="genText"&gt;&lt;strong&gt;Email&lt;/strong&gt;&lt;/td&gt;
    &lt;td class="genText"&gt;&lt;strong&gt;:&lt;/strong&gt;&lt;/td&gt;
    &lt;td class="genText"&gt;$from&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td height="20" class="genText"&gt;&lt;strong&gt;Phone&lt;/strong&gt;&lt;/td&gt;
    &lt;td class="genText"&gt;&lt;strong&gt;:&lt;/strong&gt;&lt;/td&gt;
    &lt;td class="genText"&gt;$phone&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
as you can see there are PHP variables in HTML code, When I use this code from external HTML file, they output as they are that is.. in final output $from remains as simple text $from, while it's suppose to be Name entered in Form, If I use this code in my Mail Program, it works as charm.

so, after all this long blah blah, here comes the million dollar question.. :?:

How I can read a HTML file containing PHP variable into a sinle string ?

:?

**EDIT**

okay I tried even removing all line breaks in HTML file, thus making it a single line code, but STILL THE DAMN THING DOESN'T WORK...

Posted: Wed Jan 07, 2004 9:49 am
by igoy
I'm still struggling.... but all in vein.... someone plzzzzzzzzz HELPP !!

Posted: Wed Jan 07, 2004 1:14 pm
by scorphus
This will read a file contents into a string:

Code: Select all

<?php
$fp = fopen($fileName, 'r');
$fileContent = fread($fp, filesize($fileName));
fclose($fp);
?>
Hope it helps,
Scorphus.

Posted: Wed Jan 07, 2004 3:21 pm
by Weirdan
try:

Code: Select all

function get_interpreted($file){
  $text=implode("",file($file));
  eval("\$text2send = "$text";");
  return $text2send;
}
Keep in mind that special care is needed if you're going to use quotes and $-signs in your template. perhaps this:

Code: Select all

$text=implode("",file($file));
  $text=htmlentities($text,ENT_QUOTES);
  $text=str_replace("\$","_dollar_sign",$text);
  eval("\$text2send = "$text";");
  $text2send=str_replace("_dollar_sign","\$",$text2send);
  $text2send=html_entity_decode($text2send,ENT_QUOTES);
  return $text2send;
will work (not tested though)

Posted: Thu Jan 08, 2004 5:04 am
by igoy
Thanks a lot for help guys, but I'm still where I started.
Now it's like going in circles, From my own functions to everything available on net, I tried, and I failed.

Weirdan, your Idea for handeling quotes and $ signs, logicaly looks right to me, but it doesn't seem to work. First thing I stumbled upon was [php_man]html_entity_decode[/php_man], since I'm coding for PHP ver > 4.3.0 (specifically 4.2.3 nonCSV). I found a workaround on [php_man]html_entity_decode[/php_man] in PHP manual's user notes, following function

Code: Select all

<?php

function html_entity_decode( $given_html, $quote_style = ENT_QUOTES ) {
   $trans_table = get_html_translation_table( HTML_SPECIALCHARS, $quote_style );
   if( $trans_table["'"] != ''' ) { # some versions of PHP match single quotes to '
     $trans_table["'"] = ''';
   }
   return ( strtr( $given_html, array_flip( $trans_table ) ) );
}

?>
But guess what, I just finished another round of circle.... :?

Posted: Thu Jan 08, 2004 5:32 am
by igoy
FINALLLY.............. GOT IT... :)

first of all, BIG thanks to weirdan, for showing the way.
I had almost reached to conclusion that I'm not gonna make it and got a hint from wierdan

okay here is how...

A function to get HTML file with Quotes and $ signs into a string :

Code: Select all

<?php

function getFile($file) {
// You will need to declare global variables ($ sign text) used in HTML file.
	global $var1;
	global $var2;
	global $var3;
// Now when you read and get file contents these ($ sign text) variables 
// will be changed with declared ones.
	$text=implode("",file($file));
	$text=htmlentities($text,ENT_QUOTES); 
	eval("\$text2send = "$text";"); 
	$text2send=html_entity_decode($text2send,ENT_QUOTES); 
	return $text2send;
}


/* for those who need to get html_entity_decode worked for non-CSV / PHP ver. > 4.3.0 */

function html_entity_decode( $given_html, $quote_style = ENT_QUOTES ) { 
   $trans_table = get_html_translation_table( HTML_SPECIALCHARS, $quote_style ); 
   if( $trans_table["'"] != ''' ) { # some versions of PHP match single quotes to ' 
     $trans_table["'"] = '''; 
   } 
   return ( strtr( $given_html, array_flip( $trans_table ) ) ); 
} 

?>

Posted: Thu Jan 08, 2004 5:35 am
by Weirdan
Oops, just overlooked that you actually need variables to be parsed ;). Then you need to replace only quotes:

Code: Select all

// file 'tst.php'
$text=implode("",file("text"));
$row['name']="Yabba";
     $text=str_replace("""," _double_quote_ ",$text);
     eval("\$text2send = "$text";");
     $text2send=str_replace(" _double_quote_ ",""",$text2send);
     echo $text2send;

Code: Select all

// file 'text'
Dear &#123;$row&#1111;'name']&#125;
"quote"
\$gubba

Code: Select all

weirdan@office$ php -q tst.php
Dear Yabba
"quote"
$gubba
weirdan@office$
To include literal '$' in your template just escape it with backslash as usual.

Posted: Thu Jan 08, 2004 5:39 am
by igoy
Yeh, Thanks again Weirdan.

As you can see, It's solved now... :D

yeeeeaahh, I'm a Happy Programmer.

* Dances and Hugs Monitor *
* goes Bonkers *