PHP editing Microsoft Word document str_replace an

2019-07-27 07:53发布

Assume, I've got MSWord file source.doc with next content "Content of Microsoft Word file". For example, I'd like to open it via PHP and replace word "Microsoft" to "Openoffice" and save the result into result.doc. Here is the code using preg_replace:

$content = file_get_contents( SOMEPATH . '/source.doc' );
$new_content = preg_replace( '/Microsoft/i', 'Openoffice', $content );
file_put_contents( SOMEPATH . '/target.doc', $new_content );

Or using str_replace:

$content = file_get_contents( SOMEPATH . '/source.doc' );
$new_content = str_replace( 'Microsoft', 'Openoffice', $content );
file_put_contents( SOMEPATH . '/target.doc', $new_content );

None of them doesn't work. Code runs without any exceptions, but target.doc is the same as source.doc. Replacement not performs.

I've tried a lot of different reciepts, such as regular expression modificators, iconv and so on, but nothing helps.

var_dump of $content shows raw structure of source.doc that is full of unusual characters and as I suppose some of it stops str_replace or preg_replace scanning. Can't figure out which char is it and what should I do if I'll find it.

var_dump of $new_content is identical to $content.

Thanks forward for any help!

2条回答
闹够了就滚
2楼-- · 2019-07-27 08:18

If you have a DOCX file you need to replace something in, its basically a zipped up xml archive. Here's an example on how to replace the word "Microsoft" with "Openoffice" in a DOCX file.

$zip = new ZipArchive;
//This is the main document in a .docx file.
$fileToModify = 'word/document.xml';
$wordDoc = "Document.docx";

if ($zip->open($wordDoc) === TRUE) {
    //Read contents into memory
    $oldContents = $zip->getFromName($fileToModify);
    //Modify contents:
    $newContents = str_replace('Microsoft', 'Openoffice', $oldContents);
    //Delete the old...
    $zip->deleteName($fileToModify);
    //Write the new...
    $zip->addFromString($fileToModify, $newContents);
    //And write back to the filesystem.
    $return =$zip->close();
    If ($return==TRUE){
        echo "Success!";
    }
} else {
    echo 'failed';
}

Hope this helps!

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-07-27 08:29

I think this is what you are looking for :) http://phpword.codeplex.com/ since doc files are not ordinary text files (try opening one with notepad..you'll get my point)

查看更多
登录 后发表回答