Skip to content

Instantly share code, notes, and snippets.

@cypres
Created August 25, 2011 11:32
Show Gist options
  • Save cypres/1170470 to your computer and use it in GitHub Desktop.
Save cypres/1170470 to your computer and use it in GitHub Desktop.
Palindrome tester (UTF-8 compatible)
function palindromeTest($i) {
preg_match_all('/([\\x41-\\x5A\\x61-\x7A])|([\\xC0-\\xDF].)|([\\xE0-\\xEF]..)|([\\xF0-\\xFF]...)]/',$i,$m);
$a = mb_strtolower(implode('',$m[0]),'UTF-8');
$b = mb_strtolower(implode('',array_reverse($m[0])),'UTF-8');
return ($a == $b);
}
var_dump(palindromeTest('Syy hyökätä: köyhyys!')); // true
var_dump(palindromeTest('Syy hyäkötä: köyhyys!')); // false
var_dump(palindromeTest('Selmas lakserøde garagedøre skal samles')); // true
var_dump(palindromeTest('a€b€a')); // true (U+20AC test)
var_dump(palindromeTest('a𝀸b𝀸a')); // true (U+1D038 test)
@cypres
Copy link
Author

cypres commented Aug 25, 2011

Use the git clone URLs or the download button, the gist system don't support displaying UTF-8 chars apparently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment