How does character encoding affect the output of string functions in PHP?
Character encoding can affect the output of string functions in PHP by causing unexpected behavior or errors when working with non-ASCII characters. To ensure consistent and accurate string manipulation, it's important to use the correct character encoding functions in PHP, such as mb_strlen() and mb_substr(), which are specifically designed to handle multi-byte character sets like UTF-8.
// Example of using mb_strlen() and mb_substr() with UTF-8 encoding
$string = "こんにちは世界"; // Japanese greeting "Hello World" in UTF-8
$length = mb_strlen($string, 'UTF-8');
$substring = mb_substr($string, 0, 5, 'UTF-8');
echo $length; // Output: 7
echo $substring; // Output: こんにちは