Skip to content

Commit 3c78edd

Browse files
committed
IMPROVED : Test if text is UTF-8 before converting
1 parent 6b2511e commit 3c78edd

File tree

6 files changed

+52
-20
lines changed

6 files changed

+52
-20
lines changed

src/PHPWord/Section.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,10 @@ public function getSettings() {
109109
* @return PHPWord_Section_Text
110110
*/
111111
public function addText($text, $styleFont = null, $styleParagraph = null) {
112-
$givenText = utf8_encode($text);
113-
$text = new PHPWord_Section_Text($givenText, $styleFont, $styleParagraph);
112+
if(!PHPWord_Shared_String::IsUTF8($text)){
113+
$text = utf8_encode($text);
114+
}
115+
$text = new PHPWord_Section_Text($text, $styleFont, $styleParagraph);
114116
$this->_elementCollection[] = $text;
115117
return $text;
116118
}
@@ -125,9 +127,13 @@ public function addText($text, $styleFont = null, $styleParagraph = null) {
125127
* @return PHPWord_Section_Link
126128
*/
127129
public function addLink($linkSrc, $linkName = null, $styleFont = null, $styleParagraph = null) {
128-
$linkSrc = utf8_encode($linkSrc);
130+
if(!PHPWord_Shared_String::IsUTF8($linkSrc)){
131+
$linkSrc = utf8_encode($linkSrc);
132+
}
129133
if(!is_null($linkName)) {
130-
$linkName = utf8_encode($linkName);
134+
if(!PHPWord_Shared_String::IsUTF8($linkName)){
135+
$linkName = utf8_encode($linkName);
136+
}
131137
}
132138

133139
$link = new PHPWord_Section_Link($linkSrc, $linkName, $styleFont, $styleParagraph);
@@ -179,7 +185,9 @@ public function addTable($style = null) {
179185
* @return PHPWord_Section_ListItem
180186
*/
181187
public function addListItem($text, $depth = 0, $styleFont = null, $styleList = null, $styleParagraph = null) {
182-
$text = utf8_encode($text);
188+
if(!PHPWord_Shared_String::IsUTF8($text)){
189+
$text = utf8_encode($text);
190+
}
183191
$listItem = new PHPWord_Section_ListItem($text, $depth, $styleFont, $styleList, $styleParagraph);
184192
$this->_elementCollection[] = $listItem;
185193
return $listItem;
@@ -287,7 +295,9 @@ public function addTOC($styleFont = null, $styleTOC = null) {
287295
* @return PHPWord_Section_Title
288296
*/
289297
public function addTitle($text, $depth = 1) {
290-
$text = utf8_encode($text);
298+
if(!PHPWord_Shared_String::IsUTF8($text)){
299+
$text = utf8_encode($text);
300+
}
291301
$styles = PHPWord_Style::getStyles();
292302
if(array_key_exists('Heading_'.$depth, $styles)) {
293303
$style = 'Heading'.$depth;

src/PHPWord/Section/Footer.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,10 @@ public function __construct($sectionCount) {
7272
* @return PHPWord_Section_Text
7373
*/
7474
public function addText($text, $styleFont = null, $styleParagraph = null) {
75-
$givenText = utf8_encode($text);
76-
$text = new PHPWord_Section_Text($givenText, $styleFont, $styleParagraph);
75+
if(!PHPWord_Shared_String::IsUTF8($text)){
76+
$text = utf8_encode($text);
77+
}
78+
$text = new PHPWord_Section_Text($text, $styleFont, $styleParagraph);
7779
$this->_elementCollection[] = $text;
7880
return $text;
7981
}
@@ -162,7 +164,9 @@ public function addMemoryImage($link, $style = null) {
162164
* @return PHPWord_Section_Footer_PreserveText
163165
*/
164166
public function addPreserveText($text, $styleFont = null, $styleParagraph = null) {
165-
$text = utf8_encode($text);
167+
if(!PHPWord_Shared_String::IsUTF8($text)){
168+
$text = utf8_encode($text);
169+
}
166170
$ptext = new PHPWord_Section_Footer_PreserveText($text, $styleFont, $styleParagraph);
167171
$this->_elementCollection[] = $ptext;
168172
return $ptext;

src/PHPWord/Section/Header.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,10 @@ public function __construct($sectionCount) {
7272
* @return PHPWord_Section_Text
7373
*/
7474
public function addText($text, $styleFont = null, $styleParagraph = null) {
75-
$givenText = utf8_encode($text);
76-
$text = new PHPWord_Section_Text($givenText, $styleFont, $styleParagraph);
75+
if(!PHPWord_Shared_String::IsUTF8($text)){
76+
$text = utf8_encode($text);
77+
}
78+
$text = new PHPWord_Section_Text($text, $styleFont, $styleParagraph);
7779
$this->_elementCollection[] = $text;
7880
return $text;
7981
}
@@ -162,7 +164,9 @@ public function addMemoryImage($link, $style = null) {
162164
* @return PHPWord_Section_Footer_PreserveText
163165
*/
164166
public function addPreserveText($text, $styleFont = null, $styleParagraph = null) {
165-
$text = utf8_encode($text);
167+
if(!PHPWord_Shared_String::IsUTF8($text)){
168+
$text = utf8_encode($text);
169+
}
166170
$ptext = new PHPWord_Section_Footer_PreserveText($text, $styleFont, $styleParagraph);
167171
$this->_elementCollection[] = $ptext;
168172
return $ptext;

src/PHPWord/Section/Table/Cell.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ public function __construct($insideOf, $pCount, $width = null, $style = null) {
108108
* @return PHPWord_Section_Text
109109
*/
110110
public function addText($text, $styleFont = null, $styleParagraph = null) {
111-
$text = utf8_encode($text);
111+
if(!PHPWord_Shared_String::IsUTF8($text)){
112+
$text = utf8_encode($text);
113+
}
112114
$text = new PHPWord_Section_Text($text, $styleFont, $styleParagraph);
113115
$this->_elementCollection[] = $text;
114116
return $text;
@@ -124,9 +126,13 @@ public function addText($text, $styleFont = null, $styleParagraph = null) {
124126
*/
125127
public function addLink($linkSrc, $linkName = null, $style = null) {
126128
if($this->_insideOf == 'section') {
127-
$linkSrc = utf8_encode($linkSrc);
129+
if(!PHPWord_Shared_String::IsUTF8($linkSrc)){
130+
$linkSrc = utf8_encode($linkSrc);
131+
}
128132
if(!is_null($linkName)) {
129-
$linkName = utf8_encode($linkName);
133+
if(!PHPWord_Shared_String::IsUTF8($linkName)){
134+
$linkName = utf8_encode($linkName);
135+
}
130136
}
131137

132138
$link = new PHPWord_Section_Link($linkSrc, $linkName, $style);
@@ -160,7 +166,9 @@ public function addTextBreak() {
160166
* @return PHPWord_Section_ListItem
161167
*/
162168
public function addListItem($text, $depth = 0, $styleText = null, $styleList = null) {
163-
$text = utf8_encode($text);
169+
if(!PHPWord_Shared_String::IsUTF8($text)){
170+
$text = utf8_encode($text);
171+
}
164172
$listItem = new PHPWord_Section_ListItem($text, $depth, $styleText, $styleList);
165173
$this->_elementCollection[] = $listItem;
166174
return $listItem;
@@ -269,7 +277,9 @@ public function addObject($src, $style = null) {
269277
*/
270278
public function addPreserveText($text, $styleFont = null, $styleParagraph = null) {
271279
if($this->_insideOf == 'footer' || $this->_insideOf == 'header') {
272-
$text = utf8_encode($text);
280+
if(!PHPWord_Shared_String::IsUTF8($text)){
281+
$text = utf8_encode($text);
282+
}
273283
$ptext = new PHPWord_Section_Footer_PreserveText($text, $styleFont, $styleParagraph);
274284
$this->_elementCollection[] = $ptext;
275285
return $ptext;

src/PHPWord/Section/TextRun.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,10 @@ public function __construct($styleParagraph = null) {
8080
* @return PHPWord_Section_Text
8181
*/
8282
public function addText($text = null, $styleFont = null) {
83-
$givenText = utf8_encode($text);
84-
$text = new PHPWord_Section_Text($givenText, $styleFont);
83+
if(!PHPWord_Shared_String::IsUTF8($text)){
84+
$text = utf8_encode($text);
85+
}
86+
$text = new PHPWord_Section_Text($text, $styleFont);
8587
$this->_elementCollection[] = $text;
8688
return $text;
8789
}

src/PHPWord/Template.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ public function setValue($search, $replace) {
8686
}
8787

8888
if(!is_array($replace)) {
89-
$replace = utf8_encode($replace);
89+
if(!PHPWord_Shared_String::IsUTF8($replace)){
90+
$replace = utf8_encode($replace);
91+
}
9092
}
9193

9294
$this->_documentXML = str_replace($search, $replace, $this->_documentXML);

0 commit comments

Comments
 (0)