markthien Posted March 1, 2010 Share Posted March 1, 2010 Hi guys, I got a textarea let user enter their remark and I need to get the length of user entered remark. Hence, I use strlen but it's no right when user enter both chinese and alphabet together like: $tmp = 'Hello dear'; // string from textarea $len = strlen($tmp); // this is correct where it return 10 $tmp = 'Hello dear 双皮奶'; // string from textarea $len = strlen($tmp); // this is not really correct where it return 19 I need it to return length of 13 for the 2nd one. Hwo to do that? thanks! Cheers, Mark Thien Link to comment https://forums.phpfreaks.com/topic/193750-how-to-get-actual-number-of-unicode-character/ Share on other sites More sharing options...
inversesoft123 Posted March 1, 2010 Share Posted March 1, 2010 strlen(utf8_decode($tmp)) Link to comment https://forums.phpfreaks.com/topic/193750-how-to-get-actual-number-of-unicode-character/#findComment-1019786 Share on other sites More sharing options...
inversesoft123 Posted March 1, 2010 Share Posted March 1, 2010 function unistrlen($tmp) { $foo = preg_split("//u", $tmp); $foo1 = -2; foreach ($foo as $tmp) $foo1++; return $foo1; } I am not sure abut this dirty function but try Link to comment https://forums.phpfreaks.com/topic/193750-how-to-get-actual-number-of-unicode-character/#findComment-1019790 Share on other sites More sharing options...
salathe Posted March 1, 2010 Share Posted March 1, 2010 inversesoft123, is there any reason you decided to mention using preg_split over your earlier suggestion of strlen/utf8_decode? Link to comment https://forums.phpfreaks.com/topic/193750-how-to-get-actual-number-of-unicode-character/#findComment-1019794 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.