How to find the first non-repeated character in a string with PHP

1 Answer

0 votes
function getFirstNonRepeatedCharacter($s) {
    for ($i = 0; $i <= strlen($s) - 1; $i++) {
        if (substr_count($s, substr($s, $i, 1)) == 1) {
            return substr($s, $i, 1);
        }
    }
}

echo getFirstNonRepeatedCharacter("ppdadxefe");



/*
run:

a
 
*/

 



answered Feb 26, 2019 by avibootz
edited Feb 27, 2019 by avibootz
...