How to check if a string is a palindrome ignoring case and non-alphanumeric characters in PHP

1 Answer

0 votes
function isPalindrome(string $s): bool {
    // Remove non-alphanumeric characters and convert to lowercase
    $normalized = strtolower(preg_replace("/[^a-zA-Z0-9]/", "", $s));
    echo $normalized . PHP_EOL;

    return $normalized === strrev($normalized);
}

$s = "+^-Ab#c!D 50...#  05*()dcB[]A##@!$";

echo isPalindrome($s) ? 'true' : 'false';



/*
run:

abcd5005dcba
true

*/

 



answered Aug 10, 2025 by avibootz
...