3

I reduced a php script to the exact code necessary to solve a perplexing problem (at least to me!). All the script below is supposed to do is replace '++name' in an rtf template with a hard coded variable that will print 'me' on one line, and 'you' on the next line. Besides trying "\r\n" to create the new line, I've also tried "\par" to no avail. The below code replaces '++name' with "meyou" on one line. I have found a number of "solutions" on stackoverflow and other forums, but none have worked for me.

Any help is much appreciated.

    <?php
    $name = "me" . "\r\n" . "you";
    header('Content-type: application/msword');
    header('Content-Disposition: inline, filename=filenot.rtf');
    $filename = 'rtfnotice.rtf';
    $fp = fopen ($filename, 'r');
    $output = fread( $fp, filesize($filename));
    fclose ($fp);
    $output = str_replace('++name', $name, $output);
    echo $output;
?>
Arlene K
  • 253
  • 2
  • 6
  • Since your mime type is `application/msword` I don't think plain operating system newline characters are going to do anything for you. Have you tried using a carat-P `^p`? I know MS Word used to use it for carriage returns (may still) and that could be what you're looking for. I would post this as an answer, but I can't promise it will work. Just something to try. –  Feb 13 '12 at 20:05
  • Try using `PHP_EOL` http://stackoverflow.com/questions/128560/when-do-i-use-the-php-constant-php-eol – Richard Feb 13 '12 at 20:06

2 Answers2

4

With RTF, each formatted section is enclosed in mustaches. The "formatting" is placed at the beginnning. So to make something bold, {\b hey, this text is bold}

To output "me" and "you" on seperate lines, you can put {\pard\par} between them, or you can make them their own paragraphs. {\pard me \par}{\pard you \par}

I think, you can also simply put \line. Isn't RTF fun?

Some time ago I wrote a library for outputting RTF. You may find my "format" function useful. Basically you supply a bunch of properties to the text and it outputs it in RTF. This is AS3, but pretty eays to translate to PHP.

Jonathan Rowny
  • 7,588
  • 1
  • 18
  • 26
  • Thanks to all who responded and I tried 'em all, but the code that got me 2 lines: $name = "me" . "{\pard\par}" . "you"; – Arlene K Feb 13 '12 at 21:10
  • the \pard and \par commands worked for me. And thanks, I tried \line, \n, \r and a bunch of other stuff, and nothing else worked. Thanks again. – Maximus Aug 04 '17 at 17:35
1

It's likely that this may have something to do with your content type. I'd replace application/msword with application/rtf Then your associated RTF application will at least be rendering it based on the correct MIME type.

When I tried this on my server just now, it rendered in Word with the line break just fine.

AlexC
  • 1,091
  • 13
  • 25