Guest User

Untitled

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