开发者

PHP editing Microsoft Word document str_replace and preg_replace don't work

Assume, I've got MSWord file source.doc w开发者_C百科ith 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!


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!


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜