Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 957669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:44:34+00:00 2026-05-16T00:44:34+00:00

So I need to edit some text in a Word document. I created a

  • 0

So I need to edit some text in a Word document. I created a Word document and saved it as XML. It is saved correctly (I can open the XML file in MS Word and it looks exactly like the docx original).

So then I use PHP DOM to edit some text in the file (just two lines) (EDIT – bellow is already fixed working version):

<?php

$firstName = 'Richard';
$lastName = 'Knop';

$xml = file_get_contents('template.xml');

$doc = new DOMDocument();
$doc->loadXML($xml);
$doc->preserveWhiteSpace = false;

$wts = $doc->getElementsByTagNameNS('http://schemas.openxmlformats.org/wordprocessingml/2006/main', 't');

$c1 = 0; $c2 = 0;
foreach ($wts as $wt) {

    if (1 === $c1) {
        $wt->nodeValue .= ' ' . $firstName;
        $c1++;
    }

    if (1 === $c2) {
        $wt->nodeValue .= ' ' . $lastName;
        $c2++;
    }

    if ('First Name' === substr($wt->nodeValue, 0, 10)) {
        $c1++;
    }

    if ('Last Name' === substr($wt->nodeValue, 0, 9)) {
        $c2++;
    }

}

$xml = str_replace("\n", "\r\n", $xml); 

$fp = fopen('final-xml.xml', 'w');
fwrite($fp, $xml);
fclose($fp);

This gets executed properly (no errors). These two lines:

<w:t>First Name:</w:t>
<w:t>Last Name:</w:t>

Get replaced with these:

<w:t>First Name: Richard</w:t>
<w:t>Last Name: Knop</w:t>

However, when I try to open the final-xml.xml file in MS Word, it doesn’t open (Word freezes). Any suggestions.

EDIT:

I tried using levenstein():

$xml = file_get_contents('template.xml');
$xml2 = file_get_contents('final-xml.xml');

$str = str_split($xml, 255);
$str2 = str_split($xml2, 255);

$i = 0;
foreach ($str as $s) {
    $dist = levenshtein($s, $str2[$i]);
    if (0 <> $dist) {
        echo $dist, '<br />';
    }
    $i++;
}

Which outputted nothing.

Which is weird. When I open the final-xml.xml file in notepad, I can clearly see that those two lines have changed.

EDIT2:

Here is the template.xml file: http://uploading.com/files/61b2922b/template.xml/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:44:35+00:00Added an answer on May 16, 2026 at 12:44 am

    This is a problem related to DOS vs UNIX line endings. Word 2007 does not tolerate a \n line ending, it requires \r\n whereas Word 2010 is more tolerant and accepts both versions.

    To fix the problem make sure that you replace all UNIX line breaks with DOS ones before saving the output file:

    $xml = str_replace("\n", "\r\n", $xml); 
    

    Full sample:

    <?php
    
    $firstName = 'Richard';
    $lastName = 'Knop';
    
    $xml = file_get_contents('template.xml');
    
    $doc = new DOMDocument();
    $doc->loadXML($xml);
    $doc->preserveWhiteSpace = false;
    
    $wts = $doc->getElementsByTagNameNS('http://schemas.openxmlformats.org/wordprocessingml/2006/main', 't');
    
    foreach ($wts as $wt) {
       echo $wt->nodeValue;
    
        if ('First Name:' === $wt->nodeValue) {
            $wt->nodeValue = 'First Name: ' . $firstName;
        }
    
        if ('Last Name:' === substr($wt->nodeValue, 0, 10)) {
            $wt->nodeValue = 'Last Name: ' . $lastName;
        }
    }
    
    $xml = $doc->saveXML();
    
    // Replace UNIX with DOS line endings
    $xml = str_replace("\n", "\r\n", $xml); 
    
    $fp = fopen('final-xml.xml', 'w');
    fwrite($fp, $xml);
    fclose($fp);
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.