SHARE
TWEET

Untitled

a guest Jan 18th, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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');
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top