How to Get Rid of PHP mb_convert_encoding() Deprecation Error

Published December 05, 2024

Deprecated: mb_convert_encoding(): Handling HTML entities via mbstring is deprecated; use htmlspecialchars, htmlentities Deprecated: mb_convert_encoding(): Handling HTML entities via mbstring is deprecated; use htmlspecialchars, htmlentities

On PHP 8.2, I use Parsedown library to parse Markdown content to HTML. I started using ParsedownExtra library for the additional table parsing, and then got this error.

Deprecated: mb_convert_encoding(): Handling HTML entities via mbstring is deprecated; use htmlspecialchars, htmlentities, or mb_encode_numericentity/mb_decode_numericentity instead in /PATH-TO/ParsedownExtra.php on line 628

What is this deprecation about?

The mb_convert_encoding() function is used to convert strings between UTF-8 and other encodings. It is prone to errors and is not as efficient as htmlentities() and htmlspecialchars() in converting betwen two character encoding of strings.

In our working solution, we will replace that function with htmlspecialchars_decode()andhtmlentities()`.

Solution

The solution is pretty straightforward.

Line 628 in ParsedownExtra.php contains this:

$elementMarkup = mb_convert_encoding($elementMarkup, 'HTML-ENTITIES', 'UTF-8');

Change that line to:

$elementMarkup = htmlspecialchars_decode(iconv('UTF-8', 'ISO-8859-1', htmlentities($elementMarkup, ENT_COMPAT, 'UTF-8')), ENT_QUOTES);

Save the file and refresh the webpage on which the deprecation error showed up. Upon refreshing, you should not see the error anymore.

Has this worked for you?

If this tutorial has worked for you, please let me know, and share this post. You can also contact me with any questions or post in the comments below.

Related Posts

If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.

Disclaimer: Our website is supported by our users. We sometimes earn affiliate links when you click through the affiliate links on our website.

Last Updated: December 05, 2024.     This post was originally written on December 05, 2024.