hi please can someone help?

I have designed an applicant form that posts data to session variables then at the end of the application it puts all session variables in an array.

I then have a template.rtf file which I copy and rename to $_session_timestamp.rtf

I then open for writing

I have a function that searches the opened document for placeholders %%placeholder name%% and replace with relevant array data. This works great except for one annoying issue.

Some of the array data has come from text area field with new lines needed. When I us nl2br to turn the \n to <br /> and write to rtf file it shows in the rtf as actual echoed <br /> rather than putting the text following the tag on a new line. If I don't use nl2br it prints all the data on one line. This means you get words like the decidedto rather than decided
To

Is there a way to get the formatting right? Please help in plain English I am not a good interpreter of really techie answers thanks

Dani AI

Generated

Quick summary and root cause

The problem is that nl2br (and any HTML <br /> tags) produces HTML, not RTF. RTF uses control words (for example a paragraph break is the control word \par), so inserting <br /> into an RTF file will just show the HTML text. was on the right track: you must convert real newlines into RTF control words and make sure you escape RTF-special characters. (metacpan.org)

How to do it (practical steps)

  1. Work with the raw textarea value (don’t call nl2br).
  2. Normalize newlines (CRLF / CR / LF) to one form.
  3. Escape RTF special characters in the user text (\, {, }) so user input can’t break the RTF.
  4. Replace normalized newlines with the RTF paragraph control word, and optionally wrap the inserted text in a paragraph group so formatting doesn't bleed.

Example (replace %%PLACEHOLDER%% in your template with this result):

$raw = stripslashes($valueFromSession);
$raw = preg_replace('/\r\n|\r|\n/', "\n", $raw);            // normalize
$rtfSafe = strtr($raw, ['\\' => '\\\\', '{' => '\\{', '}' => '\\}']); // escape
$rtfSafe = str_replace("\n", "\\par\n", $rtfSafe);           // newline -> RTF
$rtfSafe = "{\\pard " . $rtfSafe . " \\par}";               // keep it in a paragraph
$document = str_replace('%%'.strtoupper($key).'%%', $rtfSafe, $document);

Troubleshooting tips and common mistakes

  • Don’t strip newlines earlier (your code was turning \r\n into a space). That’s why everything ran together.
  • Fix the conditional bug (if ($value == "Select" || "" )) — use if ($value == "Select" || $value == "") or if (empty($value) || $value == "Select").
  • When serving the generated file to a browser set the MIME type to application/rtf (Word/WordPad recognize it better). (stackoverflow.com)
  • If you expect non-ASCII characters, make sure your RTF header/encoding is correct (ANSI codepage or use \uN escapes); otherwise characters may not render as expected. (en.wikipedia.org)

This approach keeps template logic simple, avoids inserting HTML into RTF, and prevents your \par tokens from being escaped away or printed literally.

Recommended Answers

All 6 Replies

Member Avatar for Member #120589

how about

$rtf = str_replace("\n",'\par ',$textarea);

Hi I tried this now and in RTF now it breaks the line but actually prints the word par any suggestions?

this is the code I put in the \n didn't work

value = str_replace("<br />","\par ",$value);

the output to rtf is this:


dsfdsfdsfspar par
fdsfdspar par
fdsfpar par
sdf

should be

dsfdsfdsfs
fdsfds
fdsf
sdf

Member Avatar for Member #120589

Could I ask, why is there a <br /> in the string anyway? If the text is coming from a textarea - don't use nl2br.

Try this:

$rtf = str_replace("\n", "\\par ", $textarea);

Hi when I tried the previous time with \n it gave me tabbed spaces so changed n to br. Then got that result I'll try this again. Thanks for your quick and continued support on this

Hi Tried this and no luck still. here is full function code

function modifier($vars, $rftfile) {
        $xchange = array ('\\' => "\\\\",
                               '{'  => "\{",
                               '}'  => "\}");
        $document = file_get_contents($rftfile);
        if(!$document) {
            return false;
        }
        foreach($vars as $key=>$value) {
            $search = "%%".strtoupper($key)."%%";
            foreach($xchange as $orig => $replace) {
                if ($value == "Select" || "" ){
                    $value ="";
                }else {
                    $value = (stripslashes($value));
                    $value = str_replace("\r\n",' ',$value);
                    
                }
                $value = str_replace($orig, $replace, $value);
            }
            $document = str_replace($search, $value, $document);
        }
        return $document;
    }
Member Avatar for Member #120589

str_replace takes arrays as parameters- so prob. no need for your loops

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.