Validation of string (isogram)

1

When creating a function that returns false if there are repeated letters in a word and true if there is not the following test passes the characters:

$this->assertTrue(isIsogram('Heizölrückstoßabdämpfung'));

But fails the following test:

$this->assertFalse(isIsogram('éléphant'));

The function that is called is as follows:

function isIsogram(string $text) {
    $letters = array_filter(str_split(strtolower($text)), function($value) {
    $v = preg_replace('/[^a-zA-Z]/i', '', $value);
    return !empty($v);
});

    $uniq = array_unique($letters);
    $diff = array_diff_assoc($letters, $uniq);

    return count($diff) > 0 ? false : true;
}

With German letters the test is applied and passes normally however the letters é are removed when using preg_replace , how can I apply this validation to characters that we use in our language?

    
asked by anonymous 10.10.2017 / 19:20

1 answer

1

One solution is to use mb_convert_case() to correctly convert all accented characters correctly. preg_replace() is responsible for removing any character that is a letter or digit.

preg_split() does the same as str_split() but does not 'chew' accented characters.

$text = 'aççao88';



function isIsogram($text) {
    $str = mb_convert_case(preg_replace('/\W/u', '', $text), MB_CASE_LOWER);
    $letters = preg_split('//u', $str, null, PREG_SPLIT_NO_EMPTY);

    $uniq = array_unique($letters);
    $diff = array_diff_assoc($letters, $uniq);

    return count($diff) > 0;
}
    
10.10.2017 / 19:30