<?php
function isPalindrome(string &$str): bool
{
return (bool) preg_match('/^(?=.)((.)(?1)\2|.?)$/', $str);
}
function sanitizePalindrome(&$str)
{
return preg_replace('/[^a-z]+/', '', strtolower($str));
}
$tests = [
'',
'radar',
'f',
'neveroddoreven',
'foo',
'palindrome',
'Red rum, sir, is murder'
];
foreach ($tests as $test) {
$test = sanitizePalindrome($test); // this is optional
printf("%20s : %s\n", $test, json_encode(isPalindrome($test)));
}
preferences:
25.28 ms | 408 KiB | 5 Q