String validation (Isogram)

Asked

Viewed 160 times

1

When creating a function that returns false if there are repeated letters in a word, and true if there is no following test passes with 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 the following:

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 letters in German the test is applied and passes normally however the letters é are removed when used the preg_replace, how can I apply this validation to characters we use in our language?

  • The problem is that it removes é or ç that?

  • yes, there is no test with ç, but with é yes...

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 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;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.