<?php
function isPalindrome(string &$str): bool
{
return (bool) preg_match('/^.$|^(.)(?:(?R)|.*?)\1$/', $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)));
}
- Output for 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
- : false
radar : true
f : true
neveroddoreven : true
foo : false
palindrome : false
redrumsirismurder : true
preferences:
64.78 ms | 406 KiB | 5 Q