IMPROVED : Test if text is UTF-8 before converting

This commit is contained in:
Progi1984 2013-12-15 12:56:40 +01:00
parent 6b2511e34e
commit 3c78edd9f4
6 changed files with 52 additions and 20 deletions

View File

@ -109,8 +109,10 @@ class PHPWord_Section {
* @return PHPWord_Section_Text * @return PHPWord_Section_Text
*/ */
public function addText($text, $styleFont = null, $styleParagraph = null) { public function addText($text, $styleFont = null, $styleParagraph = null) {
$givenText = utf8_encode($text); if(!PHPWord_Shared_String::IsUTF8($text)){
$text = new PHPWord_Section_Text($givenText, $styleFont, $styleParagraph); $text = utf8_encode($text);
}
$text = new PHPWord_Section_Text($text, $styleFont, $styleParagraph);
$this->_elementCollection[] = $text; $this->_elementCollection[] = $text;
return $text; return $text;
} }
@ -125,9 +127,13 @@ class PHPWord_Section {
* @return PHPWord_Section_Link * @return PHPWord_Section_Link
*/ */
public function addLink($linkSrc, $linkName = null, $styleFont = null, $styleParagraph = null) { public function addLink($linkSrc, $linkName = null, $styleFont = null, $styleParagraph = null) {
$linkSrc = utf8_encode($linkSrc); if(!PHPWord_Shared_String::IsUTF8($linkSrc)){
$linkSrc = utf8_encode($linkSrc);
}
if(!is_null($linkName)) { if(!is_null($linkName)) {
$linkName = utf8_encode($linkName); if(!PHPWord_Shared_String::IsUTF8($linkName)){
$linkName = utf8_encode($linkName);
}
} }
$link = new PHPWord_Section_Link($linkSrc, $linkName, $styleFont, $styleParagraph); $link = new PHPWord_Section_Link($linkSrc, $linkName, $styleFont, $styleParagraph);
@ -179,7 +185,9 @@ class PHPWord_Section {
* @return PHPWord_Section_ListItem * @return PHPWord_Section_ListItem
*/ */
public function addListItem($text, $depth = 0, $styleFont = null, $styleList = null, $styleParagraph = null) { public function addListItem($text, $depth = 0, $styleFont = null, $styleList = null, $styleParagraph = null) {
$text = utf8_encode($text); if(!PHPWord_Shared_String::IsUTF8($text)){
$text = utf8_encode($text);
}
$listItem = new PHPWord_Section_ListItem($text, $depth, $styleFont, $styleList, $styleParagraph); $listItem = new PHPWord_Section_ListItem($text, $depth, $styleFont, $styleList, $styleParagraph);
$this->_elementCollection[] = $listItem; $this->_elementCollection[] = $listItem;
return $listItem; return $listItem;
@ -287,7 +295,9 @@ class PHPWord_Section {
* @return PHPWord_Section_Title * @return PHPWord_Section_Title
*/ */
public function addTitle($text, $depth = 1) { public function addTitle($text, $depth = 1) {
$text = utf8_encode($text); if(!PHPWord_Shared_String::IsUTF8($text)){
$text = utf8_encode($text);
}
$styles = PHPWord_Style::getStyles(); $styles = PHPWord_Style::getStyles();
if(array_key_exists('Heading_'.$depth, $styles)) { if(array_key_exists('Heading_'.$depth, $styles)) {
$style = 'Heading'.$depth; $style = 'Heading'.$depth;

View File

@ -72,8 +72,10 @@ class PHPWord_Section_Footer {
* @return PHPWord_Section_Text * @return PHPWord_Section_Text
*/ */
public function addText($text, $styleFont = null, $styleParagraph = null) { public function addText($text, $styleFont = null, $styleParagraph = null) {
$givenText = utf8_encode($text); if(!PHPWord_Shared_String::IsUTF8($text)){
$text = new PHPWord_Section_Text($givenText, $styleFont, $styleParagraph); $text = utf8_encode($text);
}
$text = new PHPWord_Section_Text($text, $styleFont, $styleParagraph);
$this->_elementCollection[] = $text; $this->_elementCollection[] = $text;
return $text; return $text;
} }
@ -162,7 +164,9 @@ class PHPWord_Section_Footer {
* @return PHPWord_Section_Footer_PreserveText * @return PHPWord_Section_Footer_PreserveText
*/ */
public function addPreserveText($text, $styleFont = null, $styleParagraph = null) { public function addPreserveText($text, $styleFont = null, $styleParagraph = null) {
$text = utf8_encode($text); if(!PHPWord_Shared_String::IsUTF8($text)){
$text = utf8_encode($text);
}
$ptext = new PHPWord_Section_Footer_PreserveText($text, $styleFont, $styleParagraph); $ptext = new PHPWord_Section_Footer_PreserveText($text, $styleFont, $styleParagraph);
$this->_elementCollection[] = $ptext; $this->_elementCollection[] = $ptext;
return $ptext; return $ptext;

View File

@ -72,8 +72,10 @@ class PHPWord_Section_Header {
* @return PHPWord_Section_Text * @return PHPWord_Section_Text
*/ */
public function addText($text, $styleFont = null, $styleParagraph = null) { public function addText($text, $styleFont = null, $styleParagraph = null) {
$givenText = utf8_encode($text); if(!PHPWord_Shared_String::IsUTF8($text)){
$text = new PHPWord_Section_Text($givenText, $styleFont, $styleParagraph); $text = utf8_encode($text);
}
$text = new PHPWord_Section_Text($text, $styleFont, $styleParagraph);
$this->_elementCollection[] = $text; $this->_elementCollection[] = $text;
return $text; return $text;
} }
@ -162,7 +164,9 @@ class PHPWord_Section_Header {
* @return PHPWord_Section_Footer_PreserveText * @return PHPWord_Section_Footer_PreserveText
*/ */
public function addPreserveText($text, $styleFont = null, $styleParagraph = null) { public function addPreserveText($text, $styleFont = null, $styleParagraph = null) {
$text = utf8_encode($text); if(!PHPWord_Shared_String::IsUTF8($text)){
$text = utf8_encode($text);
}
$ptext = new PHPWord_Section_Footer_PreserveText($text, $styleFont, $styleParagraph); $ptext = new PHPWord_Section_Footer_PreserveText($text, $styleFont, $styleParagraph);
$this->_elementCollection[] = $ptext; $this->_elementCollection[] = $ptext;
return $ptext; return $ptext;

View File

@ -108,7 +108,9 @@ class PHPWord_Section_Table_Cell {
* @return PHPWord_Section_Text * @return PHPWord_Section_Text
*/ */
public function addText($text, $styleFont = null, $styleParagraph = null) { public function addText($text, $styleFont = null, $styleParagraph = null) {
$text = utf8_encode($text); if(!PHPWord_Shared_String::IsUTF8($text)){
$text = utf8_encode($text);
}
$text = new PHPWord_Section_Text($text, $styleFont, $styleParagraph); $text = new PHPWord_Section_Text($text, $styleFont, $styleParagraph);
$this->_elementCollection[] = $text; $this->_elementCollection[] = $text;
return $text; return $text;
@ -124,9 +126,13 @@ class PHPWord_Section_Table_Cell {
*/ */
public function addLink($linkSrc, $linkName = null, $style = null) { public function addLink($linkSrc, $linkName = null, $style = null) {
if($this->_insideOf == 'section') { if($this->_insideOf == 'section') {
$linkSrc = utf8_encode($linkSrc); if(!PHPWord_Shared_String::IsUTF8($linkSrc)){
$linkSrc = utf8_encode($linkSrc);
}
if(!is_null($linkName)) { if(!is_null($linkName)) {
$linkName = utf8_encode($linkName); if(!PHPWord_Shared_String::IsUTF8($linkName)){
$linkName = utf8_encode($linkName);
}
} }
$link = new PHPWord_Section_Link($linkSrc, $linkName, $style); $link = new PHPWord_Section_Link($linkSrc, $linkName, $style);
@ -160,7 +166,9 @@ class PHPWord_Section_Table_Cell {
* @return PHPWord_Section_ListItem * @return PHPWord_Section_ListItem
*/ */
public function addListItem($text, $depth = 0, $styleText = null, $styleList = null) { public function addListItem($text, $depth = 0, $styleText = null, $styleList = null) {
$text = utf8_encode($text); if(!PHPWord_Shared_String::IsUTF8($text)){
$text = utf8_encode($text);
}
$listItem = new PHPWord_Section_ListItem($text, $depth, $styleText, $styleList); $listItem = new PHPWord_Section_ListItem($text, $depth, $styleText, $styleList);
$this->_elementCollection[] = $listItem; $this->_elementCollection[] = $listItem;
return $listItem; return $listItem;
@ -269,7 +277,9 @@ class PHPWord_Section_Table_Cell {
*/ */
public function addPreserveText($text, $styleFont = null, $styleParagraph = null) { public function addPreserveText($text, $styleFont = null, $styleParagraph = null) {
if($this->_insideOf == 'footer' || $this->_insideOf == 'header') { if($this->_insideOf == 'footer' || $this->_insideOf == 'header') {
$text = utf8_encode($text); if(!PHPWord_Shared_String::IsUTF8($text)){
$text = utf8_encode($text);
}
$ptext = new PHPWord_Section_Footer_PreserveText($text, $styleFont, $styleParagraph); $ptext = new PHPWord_Section_Footer_PreserveText($text, $styleFont, $styleParagraph);
$this->_elementCollection[] = $ptext; $this->_elementCollection[] = $ptext;
return $ptext; return $ptext;

View File

@ -80,8 +80,10 @@ class PHPWord_Section_TextRun {
* @return PHPWord_Section_Text * @return PHPWord_Section_Text
*/ */
public function addText($text = null, $styleFont = null) { public function addText($text = null, $styleFont = null) {
$givenText = utf8_encode($text); if(!PHPWord_Shared_String::IsUTF8($text)){
$text = new PHPWord_Section_Text($givenText, $styleFont); $text = utf8_encode($text);
}
$text = new PHPWord_Section_Text($text, $styleFont);
$this->_elementCollection[] = $text; $this->_elementCollection[] = $text;
return $text; return $text;
} }

View File

@ -86,7 +86,9 @@ class PHPWord_Template {
} }
if(!is_array($replace)) { if(!is_array($replace)) {
$replace = utf8_encode($replace); if(!PHPWord_Shared_String::IsUTF8($replace)){
$replace = utf8_encode($replace);
}
} }
$this->_documentXML = str_replace($search, $replace, $this->_documentXML); $this->_documentXML = str_replace($search, $replace, $this->_documentXML);