phpfilems-wordedit.doc

Editing a .doc in PHP


I am trying to replace strings in a word document by reading the file into a variable $content and then using str_ireplace() to change the string. I can read the content from the file but I str_ireplace() does not seem to be able to replace the string. I assumed it would because the string is 'binary safe' according to the PHP documentation. Sorry, I am a beginner with PHP file manipulation so all this is quite new to me.

This is what I have written.

copy('jack.doc' , 'newFile.doc');
$handle = fopen('newFile.doc','rb');
$content = '';

while (!feof($handle))
{
    $content .= fread($handle, 1);
}
fclose($handle);

$handle = fopen('newFile.doc','wb');
$content = str_ireplace('USING_ICT_BOX', 'YOUR ICT CONTENT', $content);
fwrite($handle, $content);
fclose($handle);

When I download the new file, it opens as it should in MS Word but it shows the old string and not the one that should be replaced.

Can I fix this issue? Is there any better tool I can use for replacing strings in MS Word thourgh PHP?


Solution

  • I am going to opt for PHPWord www.phpword.codeplex.com as I believe teachers are going to get Office 2007 next year and also I will try and find some way to convert between .docx and .doc through PHP to support them in the mean time.