Generate HTML from PHP

This is a discussion on Generate HTML from PHP within the PHP Language forums, part of the PHP Programming Forums category; I am just wondering if it is possible to save a HTML string from PHPs? Let me elaborate more on ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 09-21-2004
Mike
 
Posts: n/a
Default Generate HTML from PHP

I am just wondering if it is possible to save a HTML string from PHPs?
Let me elaborate more on what I mean.

If you do
<?php
include 'abc.php'
?>

//abc.php
<html>
<head>
<title>Example</title>
</head>
<body>
<?php
echo "Hi, I'm a PHP script!";
?>
</body>
</html>
<?php $a_number = 5?>
<javascript ...
<?php echo $a_number ?>
/javascript>

I am looking for a way to display only HTML and javascript and have
$a_number replaced with 5.

I need something that will display resulting HTML of PHP execution. So
if there are functions or database queries in PHP, the HTML string
will reflect the result of function execution or retrieved result from
database queries. Not the function definition or database query
strings.

In other words, process a seperate PHP file and return its HTML output
like the INCLUDE function, but do not output it (instead set it to a
variable value).
Reply With Quote
  #2 (permalink)  
Old 09-21-2004
Justin Koivisto
 
Posts: n/a
Default Re: Generate HTML from PHP

Mike wrote:

> I am just wondering if it is possible to save a HTML string from PHPs?
> Let me elaborate more on what I mean.
>
> If you do
> <?php
> include 'abc.php'
> ?>
>
> //abc.php
> <html>
> <head>
> <title>Example</title>
> </head>
> <body>
> <?php
> echo "Hi, I'm a PHP script!";
> ?>
> </body>
> </html>
> <?php $a_number = 5?>
> <javascript ...
> <?php echo $a_number ?>
> /javascript>
>
> I am looking for a way to display only HTML and javascript and have
> $a_number replaced with 5.
>
> I need something that will display resulting HTML of PHP execution. So
> if there are functions or database queries in PHP, the HTML string
> will reflect the result of function execution or retrieved result from
> database queries. Not the function definition or database query
> strings.
>
> In other words, process a seperate PHP file and return its HTML output
> like the INCLUDE function, but do not output it (instead set it to a
> variable value).


ob_start();
include 'file.php';
$var=ob_get_clean();

--
Justin Koivisto - spam@koivi.com
http://www.koivi.com
Reply With Quote
  #3 (permalink)  
Old 09-22-2004
Nel
 
Posts: n/a
Default Re: Generate HTML from PHP

"Mike" <michma@gmail.com> wrote in message
news:8e9b7c06.0409211234.5170941e@posting.google.c om...
> I am just wondering if it is possible to save a HTML string from PHPs?
> Let me elaborate more on what I mean.
>
> If you do
> <?php
> include 'abc.php'
> ?>
>
> //abc.php
> <html>
> <head>
> <title>Example</title>
> </head>
> <body>
> <?php
> echo "Hi, I'm a PHP script!";
> ?>
> </body>
> </html>
> <?php $a_number = 5?>
> <javascript ...
> <?php echo $a_number ?>
> /javascript>
>
> I am looking for a way to display only HTML and javascript and have
> $a_number replaced with 5.
>
> I need something that will display resulting HTML of PHP execution. So
> if there are functions or database queries in PHP, the HTML string
> will reflect the result of function execution or retrieved result from
> database queries. Not the function definition or database query
> strings.
>
> In other words, process a seperate PHP file and return its HTML output
> like the INCLUDE function, but do not output it (instead set it to a
> variable value).


Just out of pure curiosity, why would you possibly want to do this?

Nel.


Reply With Quote
  #4 (permalink)  
Old 09-22-2004
Justin Koivisto
 
Posts: n/a
Default Re: Generate HTML from PHP

Nel wrote:
> "Mike" <michma@gmail.com> wrote in message
> news:8e9b7c06.0409211234.5170941e@posting.google.c om...
>
>>I am just wondering if it is possible to save a HTML string from PHPs?
>>Let me elaborate more on what I mean.
>>
>>If you do
>><?php
>>include 'abc.php'
>>?>
>>
>>//abc.php
>><html>
>><head>
>><title>Example</title>
>></head>
>><body>
>><?php
>>echo "Hi, I'm a PHP script!";
>>?>
>></body>
>></html>
>><?php $a_number = 5?>
>><javascript ...
>><?php echo $a_number ?>
>>/javascript>
>>
>>I am looking for a way to display only HTML and javascript and have
>>$a_number replaced with 5.
>>
>>I need something that will display resulting HTML of PHP execution. So
>>if there are functions or database queries in PHP, the HTML string
>>will reflect the result of function execution or retrieved result from
>>database queries. Not the function definition or database query
>>strings.
>>
>>In other words, process a seperate PHP file and return its HTML output
>>like the INCLUDE function, but do not output it (instead set it to a
>>variable value).

>
>
> Just out of pure curiosity, why would you possibly want to do this?


In any instance where you want to generate your output and display it
later...

My PSA project (sf.net/projects/phpsecurityadm) uses it, my CMS that I
am working on uses it, and any time that I want to create a "cached"
version of dynamic output, I do the same thing:

1. ob start();
2. generate all the HTML necessary
3. $contents=ob_get_clean()
4. open file and write $contents to it.

The ob_* functions are handy when you are generating error messages to
display later in the HTML. The ob_* functions also don't prevent the
header() function from working. Therefore, if you have a condition that
you want to use header and forward the user to another page, you can
still generate all the HTML that would be output if that condition is
not met.

--
Justin Koivisto - spam@koivi.com
http://www.koivi.com
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 07:38 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0