Guest User

Untitled

a guest
Jan 19th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. <?php
  2. class Palindrome
  3. {
  4. public static function isPalindrome($word)
  5. {
  6. $word = strtolower($word);
  7. $wordLength = strlen($word);
  8. $wordSplitPoint = ceil($wordLength / 2);
  9.  
  10. if ($wordLength % 2 == 0) {
  11. $firstHalf = substr($word, 0, $wordSplitPoint);
  12. $secondHalf = substr($word, $wordSplitPoint, $wordSplitPoint);
  13. } else {
  14. $firstHalf = substr($word, 0, $wordSplitPoint-1);
  15. $secondHalf = substr($word, $wordSplitPoint, $wordSplitPoint);
  16. }
  17.  
  18. $secondHalfReversed = strrev($secondHalf);
  19.  
  20. if ($firstHalf == $secondHalfReversed) {
  21. return TRUE;
  22. } else {
  23. return FALSE;
  24. }
  25. }
  26. }
  27. echo Palindrome::isPalindrome('Deleveled');
Add Comment
Please, Sign In to add comment