diff --git a/src/PhpWord/Shared/Text.php b/src/PhpWord/Shared/Text.php index 667d67ab4a..d07329c33c 100644 --- a/src/PhpWord/Shared/Text.php +++ b/src/PhpWord/Shared/Text.php @@ -145,7 +145,8 @@ public static function isUTF8($value = '') public static function toUTF8($value = '') { if (null !== $value && !self::isUTF8($value)) { - $value = utf8_encode($value); + $encoding = mb_detect_encoding($value,'UTF-8'); + $value = mb_convert_encoding($value,'UTF-8',($encoding===false) ? null : (string)$encoding); } return $value; diff --git a/src/PhpWord/TemplateProcessor.php b/src/PhpWord/TemplateProcessor.php index 6ef22fc538..a6141e8f66 100644 --- a/src/PhpWord/TemplateProcessor.php +++ b/src/PhpWord/TemplateProcessor.php @@ -257,7 +257,8 @@ protected static function ensureMacroCompleted($macro) protected static function ensureUtf8Encoded($subject) { if (!Text::isUTF8($subject) && null !== $subject) { - $subject = utf8_encode($subject); + $encoding = mb_detect_encoding($subject,'UTF-8'); + $subject = mb_convert_encoding($subject,'UTF-8',($encoding===false) ? null : (string)$encoding); } return (null !== $subject) ? $subject : '';