Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Palindrome
- {
- public static function isPalindrome($word)
- {
- $word = preg_replace('/[^a-zA-Z]/', '', $word);
- $word = strtolower($word);
- return $word == strrev($word);
- $wordLength = strlen($word);
- $wordSplitPoint = ceil($wordLength / 2);
- if ($wordLength % 2 == 0) {
- $firstHalf = substr($word, 0, $wordSplitPoint);
- $secondHalf = substr($word, $wordSplitPoint, $wordSplitPoint);
- } else {
- $firstHalf = substr($word, 0, $wordSplitPoint-1);
- $secondHalf = substr($word, $wordSplitPoint, $wordSplitPoint);
- }
- $secondHalfReversed = strrev($secondHalf);
- if ($firstHalf == $secondHalfReversed) {
- return TRUE;
- } else {
- return FALSE;
- }
- }
- }
- echo Palindrome::isPalindrome('Deleveled');
Add Comment
Please, Sign In to add comment