Thursday 2 August 2007

New lines vs Carriage returns

There really is not much in this post for the layman so if you are not a dyed in the wool techie your going to find this dull so I apologise to you.

I came across an interesting problem this afternoon when quietly building some PDF files using PHP (as is my want) which I would like to share. It concerns the carriage return which is an invisible character usually used to move the currently printing text onto the next line. It turns out FPDF, the PHP module I have been using to generate PDF files just doesn't like them. Instead it uses the alternative invisible character 'new line' which is great :o) hooray!

The really ticklish bit of all this is of course that these characters are both invisible so when one is having a struggle with them its really bit like wandering around in the dark. The first step when trying to sort a problem of this nature is to turn these characters into their representative codes, for this one requires the ord() function. One then needs to isolate these pesky invisible characters and decide what to turn them into.

So to cut a long story short if you are trying to print a block of text using FPDF and your line breaks are not appearing use the following little function on your text:

$text_var=str_replace(chr(13),chr(10),$text_var);

where $text_var is the variable containing your text. The function will magically strip out the ASCII character 13's (carriage return) and replace them with shiny new ASCII character 10's (new line).

A view from the rack is the personal blog of an IT manager who works for a pub company - hence beer