How to remove non-utf8 characters from string with PHP?

Spread the love

Sometimes, we want to remove non-utf8 characters from string with PHP.

In this article, we’ll look at how to remove non-utf8 characters from string with PHP.

How to remove non-utf8 characters from string with PHP?

To remove non-utf8 characters from string with PHP, we can use the mb_convert_encoding function.

For instance, we write

$text = mb_convert_encoding($text, "UTF-8", "UTF-8");

to call mb_convert_encoding to parse $text as a UTF-8 string and return a new string without the non-UTF-8 characters.

Conclusion

To remove non-utf8 characters from string with PHP, we can use the mb_convert_encoding function.

Leave a Reply

Your email address will not be published.