The beauty of PHP's Output Buffer
Robert Leitch | 27.08.2007 | Views: 359Not long ago I uderwent something of a 'career change', and took it as an opportunity to spend some time working on largely-abandoned websites.
Starting with VisaRus, I decided to give it a facelift and a CSS template.
Easier said than done. I'm very much a novice with CSS, so everything was a bit trial-and-error. However, the end result does exactly what I want it to do, so I'm happy.
I'd seen Google Maps being used on another website, and thought it could be used on my all-new version of VisaRus. Again, easier said than done. The whole process of integrating my first Google Map into the website was a bit painful. First I had to get my head round enough JavaScript to make it work (helped along immensely by the fantastic mapmaker at http://www.donkeymagic.co.uk) then I had to make it look decent in both Firefox and IE.
I discovered that not only did I have a CSS clash (it took a long time to get to the bottom of the problem - Google Maps use a lot of CSS for positioning and style, and they can be inherited from the stylesheet for the page the map is displayed in) but I also had DOCTYPE and encoding problems.
The map looked fine in Firefox but hellish in Explorer (the infowindow bubbles came out wrong, the text on the page [Russian, because my browser's language is set to Russian, apparently] came out in heiroglyphics).
With a bit of digging I found the two main problems. Google Maps need XHTML 1.0 Transitional or similar with UTF-8 page encoding.
Brilliant. All the pages on VisaRus (and the database content) is Windows-1251 (Cyrillic). Plus the many pages of .php files contain far-from valid HTML. If I just slap an XHTML delaration at the top of the page, nothing too bad happens, but I feel that if it says it's XHTML, then it should at least try to be.
Sticking in a UTF-8 character type, however, didn't work well with Russian characters, and just turned them into an indecipherable mess. I remembered wa-a-ay back when I was messing about with GD Library that I needed to do an encoding conversion to get Cyrillic text on dynamically-generated images.
Sure enough, mb_convert_encoding() was what I needed, in the form $text = mb_convert_encoding ($text, "UTF-8", "Windows-1251").
The next task was to apply this to all content on every page of the site.
That was the easy part, and where the output buffer comes in handy. PHP's output buffer basically takes all the output of a script that is to be transferred to the browser and caches it, allowing it to be manipulated before it gets to the browser. I already had a function using the output buffer in the site template, so all I had to do was add the single line of code above in order to make the whole site genuine UTF-8.
Another small hitch was that I wanted the site to validate with the W3C Markup Validator. It was full of nasty throwbacks to my days of being even worse at writing HTML than I am now. Of the most irritating and common problems was using <br> tags instead of the XHTML <br />. These errors took up lines and lines of the W3C validator's output, and there were too many of them and too many files to fix them all in.
So I added another simple line of code to the output buffer handler: $text=str_replace("<br>", "<br />", $text) - and hey presto, no more warnings about unclosed line break tags, leaving only the serious problems to be sorted.
More about fun things to do with the output buffer later!
Published in sections: Development : PHP Coding : Standards Compliance :
Comments from our visitors:
odlprqe wrote on 13.09.2008 at 21:04:23:
HDT3vo <a href="http://oiewlcmsahkd.com/">oiewlcmsahkd</a>, [url=http://qtaklwnnknch.com/]qtaklwnnknch[/url], [link=http://psekdgkombzq.com/]psekdgkombzq[/link], http://btqztpqnupzt.com/
xuucpgw wrote on 11.04.2008 at 08:04:21:
CcdexD <a href="http://gmiiicyoeugk.com/">gmiiicyoeugk</a>, [url=http://cnzgvbkozicv.com/]cnzgvbkozicv[/url], [link=http://tkqwdbpqeori.com/]tkqwdbpqeori[/link], http://okcoejmwlpsw.com/