PHP: Handling special characters and proper use of substr() vs. mb_substr()

When working with text manipulation in PHP, we may encounter challenges with special characters and proper string truncation. In this article, I will focus on a concrete problem that occurs when using the substr() function with special characters at the end of a string. I will also present the solution: the use of mb_substr() to avoid problems like this.

The problem:
When we use the substr() function in PHP to truncate a string, unexpected problems can arise, especially when special characters are involved. One of the most common problems is that special characters are replaced with question marks (?) at the end of the truncated string. This is because the substr() function is not set up to handle special characters correctly, especially when it comes to Unicode character encoding such as UTF-8.

The solution: mb_substr() to the rescue:
To avoid problems like this, it is recommended to use the mb_substr() function instead of substr(). "mb" stands for multibyte and this function is specifically designed to work with Unicode character encoding, such as UTF-8. By using mb_substr(), we can ensure that special characters at the end of a string are not damaged or replaced with question marks.

Example of correct use of mb_substr():
Here's an example of how you can use mb_substr() to truncate a string without losing special characters:

$text = "This is a string with special characters: ÆØÅ";
$trimmedText = mb_substr($text, 0, 10);
echo $trimmedText;

In this example, we use mb_substr() to truncate the $text string to the first 10 characters. The result will be "This is a", with the special characters "ÆØÅ" preserved correctly.

When working with text manipulation in PHP and encountering special characters at the end of a string, it's important to be aware of the potential problems with the substr() function. By switching to mb_substr(), you can avoid losing special characters and ensure proper handling of Unicode character encoding such as UTF-8.

Using mb_substr() ensures that special characters are preserved correctly and remain unchanged after text manipulation in PHP.