Skip to:
Content

bbPress.org

Opened 21 months ago

Closed 2 weeks ago

Last modified 2 weeks ago

#3585 closed defect (bug) (fixed)

Function utf8_encode() is deprecated since PHP 8.2

Reported by: philipjohn's profile philipjohn Owned by: johnjamesjacoby's profile johnjamesjacoby
Milestone: 2.6.14 Priority: normal
Severity: normal Version: trunk
Component: Tools - Warnings/Notices Keywords: commit
Cc:

Description

PHPCS running on PHP v8.2 gives us the following warning:

Function utf8_encode() is deprecated since PHP 8.2; Use mb_convert_encoding(), UConverter::transcode() or iconv instead (PHPCompatibility.FunctionUse.RemovedFunctions.utf8_encodeDeprecated)

It affects two places:

  • includes/replies/template.php:1103
  • includes/topics/template.php:1336

Change History (4)

#1 @probbpress
21 months ago

The deprecation of utf8_encode() and utf8_decode() in PHP 8.2 means that these functions are no longer recommended for use and will likely be removed in future PHP versions. These functions were originally used for converting strings from ISO-8859-1 to UTF-8 encoding and vice versa. However, they have been deprecated due to issues such as misleading function names, lack of error messages, and no support for detecting or converting other character encodings like UTF-16 to UTF-8.

To address this deprecation in BBPress or any PHP script, you should replace utf8_encode() and utf8_decode() with the mb_convert_encoding() function from the mbstring extension, which is more versatile and supports a wide range of character encodings. Alternatively, the iconv or intl extensions can also be used for character encoding conversion.

Here is an example of how you can replace utf8_encode() with mb_convert_encoding():

// Deprecated way
$utf8 = utf8_encode("\xa5\xa7\xb5");

// New way using mb_convert_encoding
$utf8 = mb_convert_encoding("\xa5\xa7\xb5", 'UTF-8', 'ISO-8859-2');

And for utf8_decode():

// Deprecated way
$iso88592 = utf8_decode($utf8);

// New way using mb_convert_encoding
$iso88592 = mb_convert_encoding($utf8, 'ISO-8859-2', 'UTF-8');

For the specific files you mentioned, you will need to locate the lines where utf8_encode() and utf8_decode() are used and replace them with the appropriate mb_convert_encoding() calls, ensuring you specify the correct source and target encodings​​.

Last edited 20 months ago by johnjamesjacoby (previous) (diff)

#2 @johnjamesjacoby
3 weeks ago

  • Keywords commit added
  • Milestone changed from Awaiting Review to 2.6.14
  • Owner set to johnjamesjacoby
  • Status changed from new to assigned
  • Version changed from 2.6.9 to trunk

#3 @johnjamesjacoby
2 weeks ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 7334:

Tools - Warnings/Notices: begin phasing out of utf8_encode() for PHP 8.2 and higher.

This change moves the existing utf8_encode() usages into a new common formatting function: bbp_format_user_display_name(), and uses this new function to abstract & encapsulate checking that mbstring equivalents are loaded & supported.

Props philipjohn.

In branches/2.6, for 2.6.14.

Fixes #3585.

#4 @johnjamesjacoby
2 weeks ago

In 7335:

Tools - Warnings/Notices: begin phasing out of utf8_encode() for PHP 8.2 and higher.

This change moves the existing utf8_encode() usages into a new common formatting function: bbp_format_user_display_name(), and uses this new function to abstract & encapsulate checking that mbstring equivalents are loaded & supported.

Props philipjohn.

In trunk, for 2.7.

Fixes #3585.

Note: See TracTickets for help on using tickets.