Skip to content

Instantly share code, notes, and snippets.

@justinpage
Last active February 27, 2020 23:03
Show Gist options
  • Select an option

  • Save justinpage/8749b0d3bf5bc767e65d7cc5e156f919 to your computer and use it in GitHub Desktop.

Select an option

Save justinpage/8749b0d3bf5bc767e65d7cc5e156f919 to your computer and use it in GitHub Desktop.
<?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