3v4l.org

run code in 300+ PHP versions simultaneously
<?php function getWordsWithOnlySpecifedLetters(array $dictionary, $letters) { $strSplit = preg_split('//u', $letters, null, PREG_SPLIT_NO_EMPTY); $strSplitLower = array_map('mb_strtolower', $strSplit); sort($strSplit); $result = []; foreach ($dictionary as $word) { if(mb_strlen($word) <= mb_strlen($letters)) { $wordSplit = preg_split('//u', $word, null, PREG_SPLIT_NO_EMPTY); sort($wordSplit); if (array_map('mb_strtolower', $wordSplit) === $strSplitLower) { $result[] = $word; } } } return $result; } $dictionary = ['apple', 'sample', 'api', 'pia', 'тоҷик']; echo '<pre>' . print_r(getWordsWithOnlySpecifedLetters($dictionary, "aip"), true) . '</pre>';

preferences:
52.54 ms | 402 KiB | 5 Q