Question

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?

Was it helpful?

Solution 4

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.

OTHER TIPS

I have same requirement for Edit .doc or .docx file using php and i have find solution for it. And i have write post on It :: http://www.onlinecode.org/update-docx-file-using-php/

copy('jack.doc' , 'newFile.doc');
$full_path =  'newFile.doc';
if($zip_val->open($full_path) == true)
{
    // In the Open XML Wordprocessing format content is stored.
    // In the document.xml file located in the word directory.

    $key_file_name = 'word/document.xml';
    $message = $zip_val->getFromName($key_file_name);               

    $timestamp = date('d-M-Y H:i:s');

    // this data Replace the placeholders with actual values
    $message = str_replace("client_full_name",      "onlinecode org",       $message);
    $message = str_replace("client_email_address",  "ingo@onlinecode.org",  $message);
    $message = str_replace("date_today",            $timestamp,         $message);      
    $message = str_replace("client_website",        "www.onlinecode.org",   $message);      
    $message = str_replace("client_mobile_number",  "+1999999999",          $message);

    //Replace the content with the new content created above.
    $zip_val->addFromString($key_file_name, $message);
    $zip_val->close();
}

Maybe this would point you to the right direction: http://davidwalsh.name/read-pdf-doc-file-php

Solutions I've found so far (not tested though):
Docvert - works for Doc, free, but not directly usable
PHPWordLib - works for Doc, not free
PHPDocX - DocX only, needs Zend.

If you can reach a web-service, look at Docmosis Cloud services since it can mailmerge a doc file with your data and give you back a doc/pdf/other. You can https post to the service to make the request so is pretty straight forward from PHP.

There is many way to handle word document file on linux

  1. antiword - not much effective as it converts into plain text.
  2. pyODconvert
  3. open-office or liboffice - through UNO
  4. unoconv utility - need to installation permission on server

There is one python script which is most usable for online file conversion but you need to convert those file through command line.

There is no specific and satisfied solution to handle word files by only using php code.

I hunted for a long time to reach at this suggestion.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top