From bc7b364447fff08a9446250078f493500831e844 Mon Sep 17 00:00:00 2001 From: Chris Rees Date: Tue, 23 Mar 2021 10:50:50 +0000 Subject: [PATCH] Replace ampersands and angle brackets with XML Currently, placing an ampersand into the document with search and replace results in Word being unable to open it "because we found an error with its contents". --- src/DocxMerge/Docx.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/DocxMerge/Docx.php b/src/DocxMerge/Docx.php index 8b7f2fd..90e0e05 100644 --- a/src/DocxMerge/Docx.php +++ b/src/DocxMerge/Docx.php @@ -172,7 +172,9 @@ public function findAndReplace( $key, $value ) { $this->findAndReplaceWithStyles($key, $value); return; } - + + $value = htmlspecialchars( $value ); + // Search/Replace in document $this->docxDocument = str_replace( $key, $value, $this->docxDocument ); // Search/Replace in footers and headers @@ -186,6 +188,8 @@ public function findAndReplaceFirst( $key, $value ) { if ( strpos( $this->docxDocument, $key ) === FALSE ) return; if ( strpos( $this->docxDocument, $key ) + strlen( $key ) === FALSE ) return; + $value = htmlspecialchars( $value ); + $leftPart = substr( $this->docxDocument, 0, strpos( $this->docxDocument, $key ) ); $rightPart = substr( $this->docxDocument, strpos( $this->docxDocument, $key ) + strlen( $key ) ); $this->docxDocument = $leftPart.$value.$rightPart;