Thursday, April 7, 2011

Convert RTF to TEXT with PHP and Linux

I always use UnRTF to get text from RTF files. The simplest way to convert the rtf file to html, then use the strip_tags method.
exec("unrtf test.rtf", $output);
echo strip_tags(implode('', $output));
However UnRTF is not fully compatible with UTF-8, it has problems with accents, ex. with the Hungarian accents, thus, I had to correct the Hungarian accents this way:
$from = array("Û", "û", "õ", "Õ");
$to = array("Ű", "ű", "ő", "Ő");

exec("unrtf test.rtf", $output);
echo strip_tags(str_replace($from, $to, html_entity_decode(implode('', $output), ENT_QUOTES, 'UTF-8')));

No comments:

Post a Comment