-
Notifications
You must be signed in to change notification settings - Fork 0
/
isPalindrome.php
37 lines (29 loc) · 1.09 KB
/
isPalindrome.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
/*
A palindrome is a word, phrase, verse, or sentence that reads the same backward or forward. Only the order of English alphabet letters (A-Z and a-z) should be considered, other characters should be ignored. Write a function that returns true if a given sentence is a palindrome; false otherwise.
For example, Palindrome::isPalindrome(‘Noel sees Leon.’) should return true as spaces, period, and case should be ignored resulting with 'noelseesleon' which is a palindrome since it reads same backward and forward.
*/
class Palindrome
{
public static function isPalindrome($str)
{
$str_array = str_split(trim($str));
$cleaned = '';
foreach($str_array as $letter){
if(ctype_alpha($letter)){
$cleaned .= strtolower($letter);
}
}
$size = strlen($cleaned);
$half1 = substr($cleaned, 0, $size/2);
$half2 = substr($cleaned, $size/2, $size);
if($half1 == strrev($half2)){
return true;
} else {
return false;
}
return false;
}
}
// For testing purposes (do not submit uncommented):
echo Palindrome::isPalindrome('Noel sees Leon');