From 9dfe0b0b5bf3930bc0ca89023e5fbab02f27d504 Mon Sep 17 00:00:00 2001 From: mhcwebdesign Date: Mon, 28 Aug 2023 21:32:25 +0100 Subject: [PATCH] Remove deprecated utf8_encode in PHP 8.2 --- docs/changes/1.x/1.2.0.md | 3 ++- src/PhpWord/Shared/Text.php | 6 +++++- src/PhpWord/TemplateProcessor.php | 6 +++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/docs/changes/1.x/1.2.0.md b/docs/changes/1.x/1.2.0.md index 83b20af735..70172884f7 100644 --- a/docs/changes/1.x/1.2.0.md +++ b/docs/changes/1.x/1.2.0.md @@ -34,4 +34,5 @@ - Moved documention from ReadTheDocs to MkDocs & Github Pages by [@Progi1984](https://github.com/Progi1984) in GH-2465 - Bump phpstan/phpstan-phpunit from 1.3.13 to 1.3.14 by [@dependabot](https://github.com/dependabot) in [#2457](https://github.com/PHPOffice/PHPWord/pull/2457) - Bump symfony/process from 5.4.26 to 5.4.28 by [@dependabot](https://github.com/dependabot) in [#2456](https://github.com/PHPOffice/PHPWord/pull/2456) -- Bump phpunit/phpunit from 9.6.10 to 9.6.11 by [@dependabot](https://github.com/dependabot) in [#2455](https://github.com/PHPOffice/PHPWord/pull/2455) \ No newline at end of file +- Bump phpunit/phpunit from 9.6.10 to 9.6.11 by [@dependabot](https://github.com/dependabot) in [#2455](https://github.com/PHPOffice/PHPWord/pull/2455) +- Remove deprecated utf8_encode in PHP 8.2 by [@mhcwebdesign](https://github.com/mhcwebdesign) in [#2447](https://github.com/PHPOffice/PHPWord/pull/2447) & [#2472](https://github.com/PHPOffice/PHPWord/pull/2472) \ No newline at end of file diff --git a/src/PhpWord/Shared/Text.php b/src/PhpWord/Shared/Text.php index 667d67ab4a..4a530b2e10 100644 --- a/src/PhpWord/Shared/Text.php +++ b/src/PhpWord/Shared/Text.php @@ -145,7 +145,11 @@ public static function isUTF8($value = '') public static function toUTF8($value = '') { if (null !== $value && !self::isUTF8($value)) { - $value = utf8_encode($value); + if (PHP_VERSION_ID < 80200) { + $value = utf8_encode($value); + } else { + $value = mb_convert_encoding($value, 'UTF-8', mb_list_encodings()); + } } return $value; diff --git a/src/PhpWord/TemplateProcessor.php b/src/PhpWord/TemplateProcessor.php index 3433287e19..ea90ef1d5e 100644 --- a/src/PhpWord/TemplateProcessor.php +++ b/src/PhpWord/TemplateProcessor.php @@ -257,7 +257,11 @@ protected static function ensureMacroCompleted($macro) protected static function ensureUtf8Encoded($subject) { if (!Text::isUTF8($subject) && null !== $subject) { - $subject = utf8_encode($subject); + if (PHP_VERSION_ID < 80200) { + $subject = utf8_encode($subject); + } else { + $subject = mb_convert_encoding($subject, 'UTF-8', mb_list_encodings()); + } } return (null !== $subject) ? $subject : '';