Using the PHP Function mb_strwidth()
In PHP, there are several functions to help you work with strings. These include the explode( ) and strtok( ) functions, which break a string into smaller components; the strstr( ), strchr( ), and strspn( ) functions, which find a small string that matches a mask; and the substr( ) and trim( ) functions, which cut a string to a specified width.
The mb_strwidth() function is one of five that truncates a string to a specific width and appends a trim marker to the return value. It takes two parameters: $str_string - the string to truncate; $int_start - the position in the string where trimming should begin; and $str_trim_marker – the string that is added at the end of the truncated string.
You may need to enable the mbstring module with the mbstring_enable_module() function in order to use this and other multibyte functions. Also, it is important to know that this function counts bytes and therefore, it is not as accurate as the PHP function strlen( ).
The mb_strlen() function returns the length of a string based on a given character encoding. It can take one or two parameters: $str_string and $encoding. If encoding is omitted, then internal encoding will be used. This function is useful when determining the length of an HTML form field or POST data. However, you should note that this function does not convert the internal character encoding to HTTP input character encoding and you will need to do this in your code.