I'm fairly new to PHP but learning. I'd like to use it to create .rtf documents with alternate text based on user input to a form. The basic idea is that the PHP file would read an .rtf file into a variable such as $output, and then start a series of string searches and replacements on the content of $output depending on the value of PHP variables gathered from user input, that this would create the rtf text inside of $output that could then be displayed and printed. (I've seen an example of how a simple search and replace involving PHP and .rtf could work like this, but that didn't involve selecting alternate text based on the value of a $variable in PHP. So, I'm looking on how to go to the next level of incorporating logic into generating the customized .rtf file.)

Anybody have any suggestions on how to start on this? I would think that I'd have to be able to use logic in the .rtf template for starters. For example, have in the .rtf template file that [IF $variable="Y"] this text [ELSE] alternate text [ENDIF]. The logic in the PHP file would need to read the rtf file into a $output variable and the PHP file contain the value for $variable. Then the logic in the PHP file would search the $output variable for the "[IF" string, evaluate it through the [ENDIF] string, then check the $variable value. After all of that, the PHP file would finally edit the $output variable to use the correct text and have it ready to display/print.

There would need to be three files for this, I would think.
1. The .rtf template, and
2. the regular .php file that gathers the values for the $variables, and
3. an include file that has the general logic about how to process [IF statements, OR, or AND, or other logic.

Of course, if something like this has already been done please let me know where I can find it to use. Otherwise if anyone is inclined to think about this and let me know what might work, I'd certainly appreciate it. I think that I see how the regular php file would work but I don't really know how to interact with the .rtf file (i.e. how to go about including logic in the .rtf file that the PHP file could use to manipulate.) I also don't know how to structure the include file that would need to be consulted whenever the logic strings were encountered while processing the $output variable via the php file.

If this doesn't make sense, its probably because I don't know enough to ask the right question or make the right assumptions. TIA for any help.

    2 months later

    I have the same problem.
    Do you find a solution for that ?

      I have not solved it and unfortunately haven't received any definitive help yet. As explained to me the solution would be to write a recursive function in PHP to process strings of text to parse through the logic in the .rtf file (i.e. act upon the if/endif statements compared to the values obtained from the datebase). That's way beyond me right now until I better understand string manipulation and objects.

      This is still on my "must do" list. So if you hear of anything please let me know. Thanks. Dennis

        If you're not fixed on doing this in PHP, there is a small selection of commercial applications that do this. While they are generally geared exclusively to the legal profession, they are useful to anyone that wants to create automated documents. The only drawback is that they are Windows-only (if you consider that a drawback). The most affordable is HotDocs (www.capsoft.com), and slightly more powerful is Ghostfill (www.ghostfill.com). Both are very capable.

          Thanks Jim. Actually, I'm trying to get this to work with a PHP/MySQL website so that's why I'm trying to get a php solution for it. (Plus I think it actually will be pretty simple to do once I know how to do recursive processing of strings inside of PHP.) I'll keep working on it. Any help/direction is still needed. Dennis

          Jim wrote:

          If you're not fixed on doing this in PHP, there is a small selection of commercial applications that do this....

            Write a Reply...