- mb_substr: documentation ( source)
- mb_strlen: documentation ( source)
- floor: documentation ( source)
- str_replace: documentation ( source)
- mb_strtolower: documentation ( source)
<?php
/* Напиши программу, проверяющую, является ли строка палиндромом. */
$text = 'А роза упала на лапу Азора';
$result = 'палиндром';
$text = mb_strtolower($text);
$text = str_replace(' ', '', $text);
$length = mb_strlen($text);
$halfLength = floor($length / 2);
for ($i = 0; $i < $halfLength; $i++) {
$left = mb_substr($text, $i, 1);
$right = mb_substr($text, -($i + 1), 1);
if ($left != $right) {
$result = 'не палиндром';
break;
}
}
echo "Результат: {$result}.";