Problem editing word file in PHP -


So I need to edit some text in word document. I have created a Word document and saved it as XML, it is saved correctly (I can open XML file in MS Word and it actually looks like DOCX original).

Then I use the PHP DOM to edit some text in the file (just two lines) (edit - the pre-working version of the bill has been fixed):

  & lt ;? Php $ firstName = 'Richard'; $ LastName = 'off'; $ Xml = file_get_contents ('template.xml'); $ Doc = new DOMDocument (); $ Doctor & gt; LoadXML ($ XML); $ Doc- & gt; Protected Whitespace = Wrong; $ Wts = $ doc- & gt; GetElementsByTagNameNS ('http://schemas.openxmlformats.org/wordprocessingml/2006/main', 'T'); $ C1 = 0; $ C2 = 0; Forex Currency ($ wts $ wt) {if (1 === $ c1) {$ wt-> nodeValue = '' $ FirstName; $ C1 ++; } If (1 === $ c2) {$ wt-> nodeValue = ''. $ LastName; $ C2 ++; } If ('first name' === substart ($ wt-> nodeValue, 0, 10)) {$ c1 ++; } If ('last name' === substart ($ wt-> node vale, 0, 9)) {$ c2 ++; }} $ Xml = str_replace ("\ n", "\ r \ n", $ xml); $ Fp = fopen ('last-xml.xml', 'w'); FILIT ($ FP, $ xml); Fclose ($ FP);  

It is executed properly (no error). These two lines:

  & lt; W: t & gt; First name: & lt; / W: t & gt; & Lt; W: t & gt; Last name: & lt; / W: t & gt;  

Replace with:

  & lt; W: t & gt; First name: Richard & lt; / W: t & gt; & Lt; W: t & gt; Last name: Knop & lt; / W: t & gt;  

However, when I try to open the last-xml.xml file in MS Word, it does not open (Word Freeze). Any suggestions

Edit:

I tried to use Levenstein ():

  $ xml = file_get_contents ('template.xml'); $ Xml2 = file_get_contents ('last-xml.xml'); $ Str = str_split ($ xml, 255); $ Str2 = str_split ($ xml2, 255); $ I = 0; Foreign Currency ($ SR $ s) {$ dist = levenshtein ($ s, $ str2 [$ i]); If (0 <-> $ dist) {echo $ dist, '& lt; Br / & gt; '; } $ I ++; }  

Whatever the output has not happened.

Who is weird when I open the last-xml.xml file in Notepad, then I can clearly see that these two lines have changed.

EDIT2:

Here is a template.exml file:

< P> This is a problem related to DOS vs Unix line end. Word 2007 does not tolerate \ n line closing, this requires \ r \ n while Word 2010 is more tolerant and accepts both versions .

To fix the problem, make sure that you change all Unix line breaks with DOS people before you save the output file:

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

Full sample:

  & lt ;? Php $ firstName = 'Richard'; $ LastName = 'off'; $ Xml = file_get_contents ('template.xml'); $ Doc = new DOMDocument (); $ Doctor & gt; LoadXML ($ XML); $ Doc- & gt; Protected Whitespace = Wrong; $ Wts = $ doc- & gt; GetElementsByTagNameNS ('http://schemas.openxmlformats.org/wordprocessingml/2006/main', 'T'); Forex Currency ($ wts $ wt) {echo $ wt- & gt; NodeValue; If ('first name:' === $ wt- & gt; node value) {$ wt-> nodeValue = 'First name:' $ FirstName; } If ('last name:' === substart ($ wt-> node vale, 0, 10)) {$ wt-> nodeValue = 'last name:' $ LastName; }} $ Xml = $ doc- & gt; Save xml (); // Change Unix with the end of the DOS line $ xml = str_replace ("\ n", "\ r \ n", $ xml); $ Fp = fopen ('last-xml.xml', 'w'); FILIT ($ FP, $ xml); Fclose ($ FP); ? & Gt;  

Comments