Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(php 8.2) Function utf8_encode() is deprecated in PHPOffice/PhpWord/Shared/Text.php on line 139 #2370

Closed
Bricobit opened this issue Jan 13, 2023 · 4 comments
Milestone

Comments

@Bricobit
Copy link

Bricobit commented Jan 13, 2023

Deprecated: Function utf8_encode() is deprecated in .../PHPOffice/PhpWord/Shared/Text.php on line 139

The original code is this

/**
      * Return UTF8 encoded value
      *
      * @param string $value
      * @return string
      */
     public static function toUTF8($value = '') {
         if (!is_null($value) && !self::isUTF8($value)) {
             $value = utf8_encode($value);
         }

         return $value;
     }

and the code that I have used to solve it is the following

  /**
      * Return UTF8 encoded value
      *
      * @param string $value
      * @return string
      */
     public static function toUTF8($value = ''){
         if (!is_null($value) && !self::isUTF8($value)) {
             $currentEncoding = mb_detect_encoding($value);
             if($currentEncoding){
                 $value = mb_convert_encoding($value, 'UTF-8', $currentEncoding);
             }
         }

         return $value;
     }

The error disappears but when generating the word it breaks and does not open, appears blank.
Any solution?

@Bricobit
Copy link
Author

Well if it helps someone, in the end I have solved it like this

 /**
     * Return UTF8 encoded value
     *
     * @param string $value
     * @return string
     */
    public static function toUTF8($value = '') {
        if (!is_null($value) && !self::isUTF8($value)) {
            $value = iconv('ISO-8859-1', 'UTF-8//TRANSLIT//IGNORE',$value); //to utf
        }
        return $value;
    }

If this is not the correct solution, let me know.
Thanks

@oytuntez
Copy link

This is still an issue. Should we fork the repo? PHP8 is pushing us to fork too many repos really.

@qnixdev
Copy link

qnixdev commented Jul 17, 2023

Or like this

/**
 * @param string $subject
 *
 * @return string
 */
protected static function ensureUtf8Encoded($subject)
{
    if (!Text::isUTF8($subject) && null !== $subject) {
        $subject = mb_convert_encoding($subject, 'UTF-8', mb_list_encodings());
    }

    return (null !== $subject) ? $subject : '';
}

@mhcwebdesign
Copy link
Contributor

mhcwebdesign commented Aug 28, 2023

The suggested changes as per this pull request at #2447 seem to resolve the PHP 8.2 issues for utf8_encode. Still needs some more testing.

@Progi1984 Progi1984 added this to the 1.2.0 milestone Sep 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

5 participants