PHP : Proper way to get UTF-8 character or string length
Problem :
Was trying to get a length of an unicode(UTF-8) character with PHP. Trouble is, PHP echoes back 3, which is technically correct, but not accurate.
For example :
<?php
echo strlen('黄');
?>
will return 3 instead of 1. How to get PHP to count the unicode character as 1?
Diagnostic :
UTF-8 character is multi-byte and need to tell PHP to use the proper encoding to measure the length.
Solution :
Use mb_strlen()
function with UTF-8
encoding to get the string length. For example :
<?php
echo mb_strlen('黄', 'UTF-8');
?>
Output :
1
which is accurate.
References :
See also : PHP : Extract part of a string starting from the middle
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+7.6k Golang : How to find out similarity between two strings with Jaro-Winkler Distance?
+15.3k Golang : Get the IPv4 and IPv6 addresses for a specific network interface
+14.2k Golang : Get current time from the Internet time server(ntp) example
+5.5k Nginx : Password protect a directory/folder
+4.6k PHP : Convert CSV to JSON with YQL example
+18.3k Golang : Check if os.Stdin input data is piped or from terminal
+17.1k Golang : Implement getters and setters
+16.7k Golang : Convert IPv4 address to decimal number(base 10) or integer
+6.5k Golang : Command line ticker to show work in progress
+10.3k Golang : Fuzzy string search or approximate string matching example
+6.4k Golang : Create zip/ePub file without compression(use Store algorithm)
+44.5k Golang : Encode image to base64 example