What are the potential pitfalls of using preg_replace in PHP for character encoding?

Using preg_replace for character encoding in PHP can lead to unexpected results and potential security vulnerabilities, as it may not handle all character encodings correctly. To ensure proper character encoding handling, it is recommended to use the mb_ functions in PHP, such as mb_convert_encoding(), which provides better support for multibyte character encodings.

// Example of using mb_convert_encoding() for character encoding
$string = "Hello, こんにちは";
$encoded_string = mb_convert_encoding($string, 'UTF-8', 'auto');
echo $encoded_string;