Hudzilla.org - the homepage of Paul Hudson
Contents > Multimedia Wish List | Report Bug | About Me ]

11.3     Working with RTF

This is NOT the latest copy of this book; click here for the latest version.

The Rich Text Format specification is a text file format that allows some document formatting to be saved in the document using special text tags. In a way, RTF can be considered very similar to HTML in that it stores display and style information for a document. RTF is noticeably less easy to read, and is not designed for hand editing. Instead, most word processing packages should be able to save in RTF format (this includes modern systems such as Microsoft Word 2003) so that documents can be interchanged across word processors and operating systems.

RTF is so named because you can open RTF files and read them in your favourite text editor, as they are simply formatted text. As a result, we can work with them using simple string functions. To give you an idea of what we're working with, here's the contents of an example RTF file:

{\rtf1
\b Hello world! \b0\par
\b Hello world! \b0\par
\b Hello world! \b0\par
}

That is functionally equivalent to the following HTML:

<HTML>
<P><B>Hello world!</B></P>
<P><B>Hello world!</B></P>
<P><B>Hello world!</B></P>
</HTML>

As you can see, HTML is a little more wordy but easier on the eye - and the brain! Complex RTF documents are virtually impossible to read and certainly impossible to understand, which limits its usefulness from a pure generation point of view. However, given that the PHP string-handling functions allow us to manipulate strings without having to worry about the things we do not match, we can pre-create a complex RTF file using a compliant package (e.g. Microsoft Word or OpenOffice.org), write in special keywords where we want to manipulate the text later, and simply replace/adjust those keywords inside our PHP scripts.



For example, we could change our example RTF script to this:

{\rtf1
\b [MESSAGE] \b0\par
\b [MESSAGE] \b0\par
\b [MESSAGE] \b0\par
}

Using a little bit of PHP magic, we can use str_replace() to replace all instances of [MESSAGE] with "Hello world!", "Goodbye Perl!" or anything in between. Using this method you can create complex documents in the RTF format, which, although they will be fairly large in size, will at least be editable by your visitors. There are limits to RTF, however, simply because it is a very basic format - if you want something more complicated, you need to turn to PDF.





<< 11.2.30 Making graphs   11.4 Creating PDF documents >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
juniorsatheler@hotmail.com - 07 Sep 2008

.... I look for converter information -> RTF para HTML/XHTML, like you ! PHP SOURCE CODE

juniorsatheler@hotmail.com - 07 Sep 2008

Dude Ezequias,

Eu procuro informação de convertedor sobre isto--> RTF para HTML/XHTML, like you !
Eu estou procurando informaçoes sobre isto tb --> RTF para HTML/XHTML

I found anything about but don't be satisfactory.
Eu achei algumas coisas a respeito mas insuficientes ainda...

Maybe, together we can help a other ! What you think about this ?
Talvez, juntos possamos compartilhar coisas a respeito.. o que acha?

Junior Satheler

A PHP User - 07 Sep 2008

Actually maybe PhpRtf library can help you. Look in www.phprtf.com site.

rtf_dude@ezequias.net - 07 Sep 2008

please... how can my php script read and understand a .rtf document? Maybe exist a function in php to this propose..?

I need to change some values from a rtf table and save it in a new file. I can't modify the original code.

Please sorry my so bad english... Im brazilian.
Thanks



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is ten plus three?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow