@
@
@coderiter2019-11-15 16:54:54
PHP
@coderiter, 2019-11-15 16:54:54

How to split a string into characters, taking into account accents?

Hello! Tell me, please, how to split the string character by character into an array, but so that the stresses (for example, one) in the array do not lie as a separate character, but remain with the letter?
Now it works like this, but the accents are considered a separate character.
$arrayLetters = preg_split('//u', $string, NULL, PREG_SPLIT_NO_EMPTY);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vadim, 2019-11-15
_

You can loop through the array and combine the accent character with the previous character.

$arrayLetters = preg_split('//u', "оди́н", NULL, PREG_SPLIT_NO_EMPTY);
$arrayLettersResult = [];
$stressSym = "́";

foreach ($arrayLetters as $i => $letter) {
    if ($letter == $stressSym) continue;
    if (!empty($arrayLetters[$i + 1]) && $arrayLetters[$i + 1] == $stressSym) {
        $arrayLettersResult[] = $letter . $stressSym;
    } else {
        $arrayLettersResult[] = $letter;
    }
}

var_dump($arrayLettersResult);

/*
array(4) {
  [0]=>
  string(2) "о"
  [1]=>
  string(2) "д"
  [2]=>
  string(4) "и́"
  [3]=>
  string(2) "н"
}
*/

D
Daria Motorina, 2019-11-15
@glaphire

It seems that the usual \s solves the problem)
https://www.phliveregex.com/p/uaC
preg_split('/\s/u', $input_line);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question