Answer the question
In order to leave comments, you need to log in
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
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) "н"
}
*/
It seems that the usual \s solves the problem)
https://www.phliveregex.com/p/uaCpreg_split('/\s/u', $input_line);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question