Thursday, August 25, 2005

PHP Script to variable

The following I found on a Usenet post on how to EASILY parse a PHP script’s output to a variable using output buffering:

"Daniel Loose" <nore...@web.de> kirjoitti viestissä:430cb793.9399...@news.cs.tu-berlin.de...
> Hello,
> I have this strange including problem:
> I want to read a piece of HTML, residing in some file, into a > variable, not echo it out. So far so easy - but now the HTML contains > a line of PHP. And I wish not to get the PHP code into my variable but > the parsed result, just like if the variable was a client ;-). I wish > to keep the nice ?> ...<? HTML area (with highlighting in my editor), > and therefore include etc. don't help because they would echo it out. > heredoc also doesnt help (if this may come up to your mind).
> Is there anything I can do...?!! The problem sounds so simple... the > more for such a great tool like PHP ... but... ?!!

Sounds like you could use buffering.

ob_start(); // Start output buffering.
// Instead of outputting anything, the output is now stored in a buffer.
include('yourfile.php');
// There, now the file has been included and php parsed.
// It isn't output, it goes into the buffer.
$yourfile = ob_get_clean();
// Now we read everything that has been buffered... And stops buffering.
?>



Result: the file you included has been parsed by the php engine and stored inside $yourfil, and nothing was output. Sounds like goal achieved.
-- SETI @ Home - Donate your cpu's idle time to science. Further reading at <http://setiweb.ssl.berkeley.ed u/> Kimmo Laine <eternal.erectio...@5P4Mgmail.com>

Thursday, May 12, 2005

Page templates

Many of you may have heard of SMARTY. I've used it once. I just think it makes
the hard things easy and the easy things hard. I suppose it is useful if you are
in a multi-programmer environment, but I would rather just use run of the mill
PHP rather than incorporating smarty templates.



Here is basically how I create every new website I do. I start by creating a
basic layout - or I could even purchase a cheap one from
basictemplates.com where all templates
are only $5. I also have a bunch on disk. I normally use these for ideas and
create a new one from scratch. I often use FrontPage 2003 and Adobe Photoshop to
help create the main template page, then I clean it up using
TextPad - my favorite text editor.



After determining where all the main content will be I will break the page apart
at that point into headers and footers.



Here is a basic example of what my header file will look like:


<html>

<head>

<title><?=$ptitle?></title>

</head>

<body>


My footer could be simply as follows:


</body>

</html>


Then, to create a new page, I would use the following template for each new
page:


<?

    $ptitle = 'Page Title';

    require 'header.php';

?>



Content goes here...



<?

    require 'footer.php';

?>


That, in essence is simply all there is to it. You can make your header and
footer file as elaborate as you need to. Some things you may notice here are:



  1. I set the $ptitle variable in my page file. It is inserted
    in the template between the title tags.

  2. I use the require function to include the header and footer
    files. You could also use include(), include_once(), or
    require_once()
    . See php.net for the
    differences of these functions.

Welcome

Welcome to PHP Tricks! I do a lot of PHP Programming. I just thought it would be fun to share some some tricks & tips I pick up in my day to day programming. Then, I can get feedback and maybe more ideas from readers and fellow blogsters!