Edit bookmark value in document.xml of .docx documents

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PitoPT
Forum Newbie
Posts: 2
Joined: Wed Jan 06, 2010 10:06 am

Edit bookmark value in document.xml of .docx documents

Post by PitoPT »

Hi all :D

first of all sorry for my bad English...

Microsoft's .docx files are created based on Open XML Format, so if you change the name of the document from test.docx to test.zip and open with lets say winrar you will see a bunch of XML's, and the content of the document will be inside a folder "word" in a document called document.xml. The basic structure is something like that:

Code: Select all

 
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml">
    <w:body>
        <w:p w:rsidR="00300719" w:rsidRDefault="007C7B0D">
            <w:proofErr w:type="gramStart"/>
            <w:r>
                <w:t xml:space="preserve">Hi </w:t>
            </w:r>
            <w:r w:rsidR="00591674">
                <w:t xml:space="preserve"> </w:t>
            </w:r>
            <w:bookmarkStart w:id="0" w:name="BNAME"/>
            <w:proofErr w:type="spellStart"/>
            <w:r w:rsidR="006D5D43">
                <w:t>BVALUE</w:t>
            </w:r>
            <w:bookmarkEnd w:id="0"/>
            <w:proofErr w:type="spellEnd"/>
            <w:proofErr w:type="gramEnd"/>
        </w:p>
        <w:sectPr w:rsidR="007C7B0D" w:rsidRPr="00C315C6" w:rsidSect="00300719">
            <w:pgSz w:w="11906" w:h="16838"/>
            <w:pgMar w:top="1417" w:right="1701" w:bottom="1417" w:left="1701" w:header="708" w:footer="708" w:gutter="0"/>
            <w:cols w:space="708"/>
            <w:docGrid w:linePitch="360"/>
        </w:sectPr>
    </w:body>
</w:document>
 
My question is: How can i create a function that receive the bookmark value (in this case "BNAME") as parameter and change the correspondent value (in this case "BVALUE") to something that i want...

My knowledge with PHP is not very good, and with xml is also not as good as i want and would be a great help by anyone who can help!!!

Thank you all very much in advance :wink:
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Edit bookmark value in document.xml of .docx documents

Post by AbraCadaver »

In the simplest form, but this will replace all occurrences of BNAME even if it is not a bookmark name:

Code: Select all

function replace_bookmark($old, $new, $file) {
    $doc = file_get_contents($file);
    $doc = str_replace($old, $new, $doc);
    file_put_contents($file, $doc);
}
 
replace_bookmark('BNAME', 'SOMETHINGELSE', '/path/to/document.xml');
To be more precise you could use preg_replace() to replace only bookmark names, or you might be able to use simpleXML functions for this.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
PitoPT
Forum Newbie
Posts: 2
Joined: Wed Jan 06, 2010 10:06 am

Re: Edit bookmark value in document.xml of .docx documents

Post by PitoPT »

Hi AbraCadaver! Thanks for the reply..

for now, i have something like that implemented, but what i need is to change the Bvalue where the Bname is... Maybe with some xml class i could do that but i am learning how and if someone could help me, would be great :)
Post Reply