Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MS Word read modify write

Status
Not open for further replies.

GeckoNZ

IS-IT--Management
Jul 23, 2001
31
I've tried reading an MS Word document fread(), replacing text e.g. <<Placeholder>> with a str_replace() and writing it back. A bit like a poor man's mailmerge. But the document isn't readable after the operation. I've tried the same thing with a text editor and replacing just 1 character has the same effect.

I was probably a bit optimistic that it might be that simple. I assume I upset a CRC check or the like.

Any suggestions.
 
Try this:
<?php
// Read/write/modify MSWordfile //
// By Karl Otto Sostack, karlos(at)johnsen-offset.dk //
function read_word($filename, $word, $replacement)
{
$fh = fopen($filename, &quot;rb&quot;);
$filecontent = fread($fh, 268435455); //256 kB filesize
fclose($fh);
unlink($filename);
$fh = fopen($filename, &quot;ab+&quot;);
$filecontent = str_replace($word, $replacement, $filecontent);
fwrite($fh, $filecontent);
fclose($fh);
read_word(&quot;./testword.doc&quot;, &quot;This&quot;, &quot;That&quot;);
?>



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top