Find all letters not used in the string

I want to be able to do something like this:

$str="abc";
echo findNot($str); //will echo "defghijklomnopqrstuvwxyz"
$str2="happy birthday";
echo findNot($str2); //will echo "cfgjklmnoqsuvwxz"

Basically, it will find all the letters not represented in the string and return them to an array or string.

I could do this easily with foreachcharacter arrays as well, but I was wondering if anyone had a more elegant solution.

+5
source share
4 answers

Here is what I came up with.

function findNot($str){
    return array_diff(range('a','z'), array_unique(str_split(strtolower($str))));
}
+5
source

How about this

$str="abc";
var_dump(findNot($str));

function findNot($string)
{
    $letters = range('a', 'z');
    $presents = array_map(function($i) { return chr($i); }, array_keys(array_filter(count_chars($string))));

    return array_diff($letters, $presents);
}

PS: imploderesult if you need a character string, not an array

PPS: not sure if this is a "more elegant" solution :-)

PPPS: another solution I could think of is

$str="abc";
var_dump(findNot($str));

function findNot($string)
{
    $letters = range('a', 'z');
    $presents = str_split(count_chars($string, 4));
    return array_intersect($letters, $presents);
}
+4
source

- :

$text = 'abcdefghijklmnop';
$search = array('a','b','c');

$result = str_replace($search, '', $text);
+3

Zerk . , .

<?php

$alphabet = 'abcdefghijklmnopqrstuvwxyz';
$alphabet = preg_split('//', $alphabet, -1, PREG_SPLIT_NO_EMPTY);

$str="abc";
var_dump( findNot($str) ); //will echo "defghijklomnopqrstuvwxyz"
$str2="happy birthday";
var_dump( findNot($str2) ); //will echo "cfgjklmnoqsuvwxz"

function findNot($str)
{
    global $alphabet;

    $str   = str_replace(' ', '', $str);
    $chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
    $chars = array_unique($chars);
    sort($chars);

    $diff = array_diff($alphabet, $chars);

    return $diff;
}
0

All Articles