Answer the question
In order to leave comments, you need to log in
How to make a function that correctly handles both Cyrillic and Latin?
Good day. I solve the problem of checking the incoming string for a palindrome (read in both directions: Argentina beckons a black man). I will convert to one register, cut out all unnecessary possible characters through str_replace.
function transformString($string){
$charsToDelete = ['!', '?', ' ', '.', ',', '-', '—'];
$string = mb_strtolower($string);
$string = str_replace($charsToDelete, '', $string);
return $string;
}
function reverseString($string){
$string = str_split($string,2);
$string = array_reverse($string);
$string = implode($string);
return $string;
}
$string = "Аргентина манит негра!";
function isPalindrom ($string) {
$textForCheck = transformString($string);
$reversedTextForCheck = reverseString($textForCheck);
echo $textForCheck.'<br>'; //аргентинаманитнегра
echo $reversedTextForCheck.'<br>'; //аргентинаманитнегра
if ($textForCheck === $reversedTextForCheck) {
echo $string . ' — эта строка палиндром.';
} else {
echo "Не полиндром.";
}
return $textForCheck;
};
function transformString($string){
$charsToDelete = ['!', '?', ' ', '.', ',', '-', '—'];
$string = mb_strtolower($string);
$string = str_replace($charsToDelete, '', $string);
return $string;
}
function reverseString($string){
$string = str_split($string,2);
$string = array_reverse($string);
$string = implode($string);
return $string;
}
isPalindrom($string); // Аргентина манит негра! — эта строка палиндром.
Answer the question
In order to leave comments, you need to log in
php.net/manual/ru/function.mb-split.php also
works correctly with Unicode multibyte characters.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question