template not finding strings with spellcheck?

Oct 28, 2010 at 11:34 AM

I'm having weird issues with the setValue function when working with word templates.

At first, it seemed that PHPWord was randomly skipping some setValue requests, despite the fact the search string matched the equivelant string in the word document.

After a couple of hours playing, I finally worked out that if a string was flagged as misspelled by the word spellchecker, the string would not be replaced.

I resolved the issue by simply turning off the spellchecker, and retyping all the strings.  It sounds bizarre, and totally unfeasable, but it did work. 

I can only assume that word somehow flags mispelled words in the xml when you save the file, which then interferes with PHPWord's parsing. I don't have time to go into the xml, but I presume this is the case.

So, if anybody has any issues with setValue not finding strings, try turning off spellcheck. It might just work

Nov 16, 2010 at 3:48 AM

i believe you are exactly correct. I was investigating some issues where only 3 of my values would be replaced. I also saw some other users in the issue tracker area that had weird issues with value replacement.  After the above suggestion i looked at the XML and there are oddities.  Below i've pasted in a relevant example from a document i created.  The "firstName", "lastName", and "streetAddress" variables didn't get replaced while the "city", "state", and "zip" ones did.  They also don't have the spellchecker XML around them marking them as not spelled correctly:

original as seen in the word document:
To: ${firstName} ${lastName}
${streetAddress}
${city}, ${state} ${zip}

XML Version:

<w:p w:rsidR="00E54A02" w:rsidRDefault="00E54A02">
<w:r>
<w:t>To: ${</w:t>
</w:r>
<w:proofErr w:type="spellStart"/>
<w:r>
<w:t>firstName</w:t>
</w:r>
<w:proofErr w:type="spellEnd"/>
<w:r>
<w:t>} ${</w:t>
</w:r>
<w:proofErr w:type="spellStart"/>
<w:r>
<w:t>lastName</w:t>
</w:r>
<w:proofErr w:type="spellEnd"/>
<w:proofErr w:type="gramStart"/>
<w:r>
<w:t>}</w:t>
</w:r>
<w:proofErr w:type="gramEnd"/>
<w:r>
<w:br/>
<w:t>${</w:t>
</w:r>
<w:proofErr w:type="spellStart"/>
<w:r>
<w:t>streetAddress</w:t>
</w:r>
<w:proofErr w:type="spellEnd"/>
<w:r>
<w:t>}</w:t>
</w:r>
<w:r>
<w:br/>
<w:t>${city}, ${state} ${zip}</w:t>
</w:r>
</w:p>

Sep 14, 2011 at 9:18 AM

To fix the problem without changing anything in word, I've edited few things in the Template class : 

adding static function "striptags"

 

private static function striptags($matches)
{
	return strip_tags($matches[1]);
}

 

Then add this line at the end of the constructor

 

$this->_documentXML = preg_replace_callback('/(\$\{.*\})/U',"self::striptags", $this->_documentXML );

 It will remove all tags added by spellcheckers for each vars

 

Nov 19, 2011 at 4:15 AM
Edited Nov 19, 2011 at 5:28 PM

Thank you airmoi for the great solution!

However, while I had an issue along these lines as well, I was getting things like:

<w:t>$</w:t>
...
<w:t>{</w:t>
...
<w:t>token}</w:t>

in the XML of my template docx.

by changing:

$this->_documentXML = preg_replace_callback('/(\$\{.*\})/U',"self::striptags", $this->_documentXML );

to: 

$this->_documentXML = preg_replace_callback('/(\$.*\})/U',"self::striptags", $this->_documentXML );

Solved the problem for me (using airmoi's solution)!

WARNING!: this only works if you don't use "$" anywhere else in your template, if you DO, set the system up to use a different symbol as a delimiter, or replace all '$' symbols with something you can replace during runtime as well (like ${dollarsign})!

Dec 17, 2011 at 8:28 PM

excuse me for my bad english...

i have exact same problem and i don't know where should i add these Functions you said!

please help me...

thank you

Ehsan Saraee

Dec 22, 2011 at 2:48 PM

The above is added/modified in the Template.php file within the "PHPWord" folder of the source code.

  1. In mine, the "$this->_documentXML" line was added after line 74, before the closing brackets of __Construct
  2. Airmoi's solution was added at line 133, just before the closing brackets of the class.

I'm not sure if your's will be exactly the same, as I have a couple of other things going on in there as well.

Mar 3, 2013 at 12:56 AM
xomby and airmoi, you're the man!!! Many thanks