To shuffle the words use str_shuffle()
.
To compare a shuffled string to a wordlist, you can use count_chars()
.
class WordFinder
{
protected $_wordList;
protected $_map;
public function __construct(array $wordList)
{
$this->_wordList = $wordList;
}
protected function _initMap()
{
if(!is_array($this->_map)) {
$this->_map = array();
foreach($this->_wordList as $word) {
$key = count_chars($word, 3);
if(!isset($this->_map[$key])) {
$this->_map[$key] = array();
}
$this->_map[$key][] = $word;
}
}
}
public function findWords($searchWord)
{
$searchWord = count_chars($searchWord, 3);
$this->_initMap();
if(isset($this->_map[$searchWord])) {
return $this->_map[$searchWord];
}
return false;
}
}
Sonra yapın
$list = array('evil', 'live', 'vile', 'cat');
$finder = new WordFinder($list);
var_dump($finder->findWords('evli'));
Ve bu dönecektir
array(3) {
[0]=>
string(4) "evil"
[1]=>
string(4) "live"
[2]=>
string(4) "vile"
}
EDIT
I've exchanged the original code with this version, as it performs much better with large wordlists. I've tested the above on my 2,2 Ghz Dual Core and it would finish 10000 calls to findWords() in a collection of 10000 words in a mere 0.08 seconds. The other version would take 207 seconds. See the revision for the old version.