3

So on my webpage I have an input form (with 'accept-charset="UTF-8"') for the user to enter text, which will get posted to a php page. In the php page, the input should be separated by spaces, then stored in an array, with a key value which equals the total count of letters in that word. There is no worry about words with the same length.

Example:

User inputs "def ghijk a bc"

  • $stringArr[1][0] = "a"

  • $stringArr[2][0] = "bc"

  • $stringArr[3][0] = "def"

  • $stringArr[5][0] = "ghijk"

I have this working all fine when it comes to English letters, but if I try to input letters from other languages, the key value is always wrong. Take this letter from Hindi for example: "मैं", it gives it a key value of 21 instead of just 1. What is the workaround to this?

$string = "मैं मैंमैं";

$stringLenArr = preg_split('/\s+/', $string);

$stringArr = array();

foreach($stringLenArr as $value) {
    // What I used before:
    // $stringArr[strlen($value)][] = $value; 

    // Attempted solution which still only works with English
    $stringArr[mb_strlen($value, "UTF-8")][] = $value;
}

Just to make things clearer: There should only ever be a single language entered, so if the solution requires a language selection box on the input page, that is no issue.

Badger
  • 301
  • 3
  • 15
  • Have you tried `iconv_strlen()` ? http://php.net/manual/en/function.iconv-strlen.php – Stuart Jul 16 '16 at 15:15
  • This still doesn't seem to work... `iconv_strlen($string, "UTF-8");` where $string = "मैं" is returning a value of 3 instead of 1. – Badger Jul 16 '16 at 21:47
  • Possible duplicate of [strlen() php function giving the wrong length of unicode characters](http://stackoverflow.com/questions/15829554/strlen-php-function-giving-the-wrong-length-of-unicode-characters) – miken32 Mar 23 '17 at 21:07

0 Answers0