fork download
  1. <?php
  2.  
  3.  
  4.  
  5. $text = "А роза упала на лапу Азора";
  6. $result = 'палиндром';
  7.  
  8. //нужно убрать пробелы и перевести буквы в нижний регистр
  9. $text = mb_strtolower($text);
  10. $text = preg_replace('/\s/', '', $text);
  11.  
  12. echo $text;
  13.  
  14. $lenght = mb_strlen($text);
  15. $halfLenght = floor($lenght/ 2); //floor() округляет дробное число в меньшую сторону до целого
  16. $lastCharIndex = $lenght - 1;
  17.  
  18. for ($i=0; $i <= $halfLenght ; $i++) {
  19. //здесь проходим по слову, берем по букве с 2 концов слова и сравниваем
  20. if ($text[$i] == $text[$lastCharIndex - $i]) {
  21. echo "This is {$result}";
  22. break;}
  23. else {
  24. echo "This is NOT {$result}";
  25. }
  26. }
Runtime error #stdin #stdout #stderr 0.01s 82880KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
PHP Fatal error:  Uncaught Error: Call to undefined function mb_internal_encoding() in /home/RdGWIG/prog.php:4
Stack trace:
#0 {main}
  thrown in /home/RdGWIG/prog.php on line 4