By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,086 Members | 1,442 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,086 IT Pros & Developers. It's quick & easy.

Generate HTML from PHP

P: n/a
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).
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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 - sp**@koivi.com
http://www.koivi.com
Jul 17 '05 #2

P: n/a
Nel
"Mike" <mi****@gmail.com> wrote in message
news:8e**************************@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.
Jul 17 '05 #3

P: n/a
Nel wrote:
"Mike" <mi****@gmail.com> wrote in message
news:8e**************************@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 - sp**@koivi.com
http://www.koivi.com
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.