Solved

Placing HTML

Posted on 2002-04-08
4
191 Views
Last Modified: 2006-11-17
What i have is a block of html in a function called log

what i want to be able to do is return this block of html as a single string, who is this done.

function log()
{
?>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
    <tr>
      <td width="100%" height = "10"><!--Spacor to top of table-->
      </td>
    </tr>
    <tr>
<?
}

this is what the function does at the mo i just echo the function but i now need to be able to return the whole html code as a single string.  This is not the full list the real function has arround 100 lines of html code.

thanks kieran
0
Comment
Question by:kplonk
  • 2
4 Comments
 
LVL 32

Expert Comment

by:Batalf
Comment Utility
Maybe you could try this:
function log()
{
ob_end_flush(); // Maybe you don't need this line
ob_start(); // Start buffering output
?>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
   <tr>
     <td width="100%" height = "10"><!--Spacor to top of table-->
     </td>
   </tr>
   <tr>
<?
$myString = ob_get_contents(); // Put buffer into a string
ob_end_clean(); // Clearing buffer
return $myString;
}

What I have done is to start buffering the output, and at the end put the buffer into a string using ob_get_content();

Hopefully, this could help you out.

Batalf
 
0
 
LVL 5

Accepted Solution

by:
andriv earned 20 total points
Comment Utility
Why not store it in a variable:
(I recommend using 'here docs' so you do not have to escape quotes)

<?
function log()
{
$myHTML =<<<EndHere
<table border="0" cellpadding="0" cellspacing="0" width="100%">
   <tr>
     <td width="100%" height = "10"><!--Spacor to top of table-->
     </td>
   </tr>
   <tr>
EndHere;
return $myHTML;
}

Now you can echo to display. And if you want to insure it's a string you can use:

$myHTML = stringval($myHTML);



0
 

Author Comment

by:kplonk
Comment Utility
Thanks so much, works a treat just one small question the EndHere is a line name right?? does this have to be left most ie no chars proceding the line name??
0
 
LVL 5

Expert Comment

by:andriv
Comment Utility
No EndHere indicates the end of the string. The way the 'Here Docs' works is you variable = followed by three < followed by a flag (it can be anything you want)

$variable =<<<StopHere

What this means vaiable = everything until you see the end flag 'StopHere'.  Then when you finish with the string you indicate it with 'StopHere;' on the last line.

$varaible =<<<Finished
Put
your
data
here
Finish;

The beauty of the 'here docs' is you don't have to escape quotes and when you print it, it will send it as you typed it. You don't have to use \n to start new lines.

I am glad I was able to help.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now