• The eternal confessions of a beautiful mind...
  • DamianM.Co.UK
  • Home
  • About
  • Archives
  • Contact
  • Sitemap
  • My Flickr

    IMG_4129IMG_4123IMG_4112IMG_4111IMG_4099IMG_4098IMG_4097IMG_4095IMG_4094IMG_4093IMG_4092IMG_4091IMG_4090IMG_4089IMG_4088IMG_4087IMG_4086IMG_4085IMG_4084IMG_4083
  • Recent Posts

    • Pump My Ride
    • Can you spell …
    • End of an E.ON
    • Drumming Ape Gets New Gig
    • Star wars explained by a 3 year old
    • Gene Hunt “Bunny Killer”
    • Coffee Table
    • Need Glasses?
    • Third Nipple
    • Helpdesk of the darkside
    • Optical Illusion
    • Dirty mind test
    • Silly funny video
    • Dalek Cam
    • Autobiographies
  • My Tools

    • RSS_Sticky
    • WordPress.org
    • WP_BlogNetworking
    • WP_BoilerPlate
    • WP_Censor
    • WP_DeliciousPost
    • WP_EasyReply
    • WP_HeadNFoot
    • WP_LinkIt
    • WP_LinkSync
    • WP_OneInstall
    • WP_PostDate
    • WP_PostNotes
    • WP_Spoiler
    • WP_StumbleDigest
    • WP_Submission
  • My Web

    • ASPAlliance
    • ClaimID
    • Digg
    • DSLRBlog
    • DVDProfiler
    • Flickr
    • Honeyed SPAM
    • My Blog
    • My company
    • MYSpace
    • MySQL
    • WordPress.org
    • YouTube
    « Recommendation : TinyMCE Plug-ins for WordPress
    Recommendation : Darkly Dreaming Dexter / Dexter Season One »

    PHP Tip: Output Control Functions

    Over the last year or so I’ve been getting used to programming in PHP, and I’ve long been at a stage when I can say my PHP skills are as good if not better than my skills in ASP.NET or ASP, an that I can accomplish more or less anything I need to in PHP.

    However I would not say that I know PHP inside out, there are many function around the margins that you would not use day to day that might be very useful. There are times when I need to do things that send me trawling though the PHP site to find anything that will make the task possible or at least a little easier. But as with anything if you don’t use it regular you will soon forget what you can do and you have to research it again the next time.

    Here I plan to document everything that I discover, no matter how little as a quick reference for me, and hopeful some sort of help to others.

    I do a lot of work with WordPress which involves using its API. This can be a little frustrating sometimes, nothing more so that when you find the feature you need to it outputs directly to the screen, and there is no alternate function that allows you to execute the function into a variable. A couple of functions like this are next_posts_link and previous_posts_link that are used to display next and previous buttons on the site. For me they work fine individually and only display is needed, but I’d like to know if neither are shown so i can do something else. After trawling the API for an alternative and finding nothing, and not wanting to write a non API solution that may not be compatible with future release, I fell upon a kludgy but very workable solution.

    PHP has a set of Output Control Functions that allow interception of any screen output into a buffer. There are many function in this section that maybe useful, but the ones i found useful are

    • ob_start: which start the interception of all output.
    • ob_get_contents: that lets you read all the buffered output up to this point into a variable.
    • ob_end_clean: that finishes the buffering of the out put.

    Here is an example of how I used these commands to solve the WordPress problem

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    
     ob_start();
     
     next_posts_link('« Previous');
     
     $previous = ob_get_contents();
     
     ob_end_clean();
     
     ob_start();
     
     previous_posts_link('Next »');
     
     $next = ob_get_contents();
     
     ob_end_clean();
     
    ?>

    This entry was posted on Wednesday, January 23rd, 2008 at 1:55 pm and is filed under Coding. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

    Leave a Reply