FlipsideReality Once upon a time, in a land far far away…

26Jul/070

Stripping extra rtf formatting with PHP

I was trying to replace markup in rtf files. The idea is that clients can setup template files with markup in them using predefined format like:

[code]
Dear **CustFName**

Thank you for your email of **LastEmailDate** which we have now processed...
[/code]

and save them as rtf files. then they can upload the template and have it merged with the data selection from the database. The problem is that word adds unneeded formatting if you re-edit within the markup, so i had the markup **email2**, and opened the rtf file and inserted 77 in the middle making **em77ail2** and saved, then when you look at the rtf file word produces you get the string:

$string = '{\rtlch\fcs1 \af39\afs20 \ltrch\fcs0 \fs20\insrsid14905953 **Em}{\rtlch\fcs1 \af39\afs20 \ltrch\fcs0 \fs20\insrsid7171971 77}{\rtlch\fcs1 \af39\afs20 \ltrch\fcs0 \fs20\insrsid14905953 ail2**}';

the solution was kindly provided by ebosscher of experts-echange:

[php]
function BreakupMonkier($monkier)
{
$retval = "";

for($index = 0; $index < strlen($monkier); $index++)
{
$retval .= substr($monkier, $index, 1) . '.*?';
}

return $retval;
}

$monkier = 'Email2';
$replacement = 'somedude@gmail.com';
$input
= '{\rtlch\fcs1 \af39\afs20 \ltrch\fcs0 \fs20\insrsid14905953
**Em}{\rtlch\fcs1 \af39\afs20 \ltrch\fcs0 \fs20\insrsid7171971
77}{\rtlch\fcs1 \af39\afs20 \ltrch\fcs0 \fs20\insrsid14905953 ail2**}';
$pattern = '/\*.*?\*.*?' . breakupMonkier($monkier) . '\*.*?\*/';

$output = preg_replace($pattern, $replacement, $input);

echo $input . "\r\n";
echo $output . "\r\n";
[/php]

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
Tagged as: Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.