1

I have a function on my php site that fills rtf templates with customer data. It replaces placeholders that look like this %%PLACEHOLDER%%. Now I want to replace a placeholder with a string that has tabs and line breaks in it. I can't figure out how to do this. I did read this post but the anwser doesn't work for me. In the final file it just displays {\pard\par} in plain text.

Replace function:

function populate_RTF($vars, $doc_file)
{

 $replacements = array ('\\' => "\\\\",
                       '{'  => "\{",
                       '}'  => "\}");

$document = file_get_contents($doc_file);
if(!$document) {
    return false;
}

foreach($vars as $key=>$value) {
    $search = "%%".strtoupper($key)."%%";

    foreach($replacements as $orig => $replace) {
        $value = str_replace($orig, $replace, $value);
    }

    $document = str_replace($search, $value, $document);
}

return $document;
}

File where I use the function:

function MakeString()
{
    foreach(/*for each here*/)
    {
        $string = $name. "\tab" . $gpu . "\tab" . $cpu . "{\pard\par}";
        $string .= $name. "\tab" . $gpu . "\tab" . $cpu . "{\pard\par}";
        $string .= $name. "\tab" . $gpu . "\tab" . $cpu . "{\pard\par}";
    }
    return $string;
}


$vars = array
(
    'name' =>                    $info['name'],
    'address' =>                 $info['address'],
    'phone' =>                   $info['phone'],
    'string-here' =>             MakeString(),
);


$new_rtf = populate_RTF($vars, $template);
$fr = fopen($saveLocation.$filename, 'w') ;
fwrite($fr, $new_rtf);
fclose($fr);

RTF output now is:
John \tabAsus GTX 1060\tabIntel I7{\pard\par}Mike\tabAsus GTX 1080\tabAMD FX 8120{\pard\par}

RTF output should be:
John        Asus GTX 1060        Intel I7
Mike        Asus GTX 1080        AMD FX 8120

Someone that can help me with this?

Bas Peters
  • 43
  • 3
  • 1
    Have you tried to use just \t instead of \tab and what is \pard ? And they need to be inside of " (double quotes) to work. – Igor Ilic May 18 '18 at 13:36
  • @IgorIlic Thanks for your reply! \t worked for tabs, thank you very much! the pard thing was the anwser in question I linked. Do you also know how I can add a line break? \n didn't work – Bas Peters May 18 '18 at 14:06
  • Can you try \r\n or
    for line break ?
    – Igor Ilic May 18 '18 at 14:46

0 Answers0