Last active
February 27, 2020 23:03
-
-
Save justinpage/8749b0d3bf5bc767e65d7cc5e156f919 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $input = array( | |
| "taco cat", | |
| "madam", | |
| "racecar", | |
| "A man, a plan, a canal, Panama!", | |
| "Palindrome", | |
| "Was it a car or a cat I saw?", | |
| "No 'x' in Nixon", | |
| "Coding Horror", | |
| ); | |
| function sanitize($item) { | |
| $pattern = "/[^a-zA-Z0-9]/"; | |
| return strtolower(preg_replace($pattern, "", $item)); | |
| }; | |
| function isPalindrome($item) { | |
| $item = sanitize($item); | |
| for ($i = 0; $i < strlen($item)/2; $i++) { | |
| $j = strlen($item) - $i - 1; | |
| if ($item[$j] !== $item[$i]) { | |
| return false; | |
| } | |
| } | |
| return true; | |
| }; | |
| $output = array_filter($input, "isPalindrome"); | |
| print_r($output) | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment