Wordsearch php – Words to search for

As from the main project, wordsearch, here is the part that will either load the words from a xml file or from a input string on a web page. The grid class can be found here.

The basics of the word class, is to load a xml file (or via a user input string/s) and then generate words to search from the words that have either been loaded or inputted.

The word class can again be inherited so that each part can be re-done differently if there is another way to open a file etc.

I have included comments in the code, the __construct and __destruct are what happens when the class is created (via the “new” command) and destroyed respectively. Just a nice way to create the class/object and also clean up after itself.

The parameters that are passed to the __construct have default values, words.xml and 5, these can be over written when creating the class as shown in the code (instead of the maxSearchNum being 5 it is 3. But this gives more control over the construction of the class.

<?php
 
class Word {
    /* private section of the class word */
 
    private $_words;
 
    /* constructor for the word class 
       default action to load a xml file to gain 5 words
    */
 
    public function __construct($wordsOrFilename = "words.xml", $maxSearchNum = 5)
    {
	$this->_words = array();
 
	// if the filename is actually words from the input screen then just add in them instead
	if (is_array($wordsOrFilename)) 
	{
	  $_loadedWords = $wordsOrFilename;
	}
	else
	{
	  // load the file of words into a array
	  $_loadedWords = $this->loadWords($wordsOrFilename);
	}
	// create a array of words to be searched for, max number as above
	$this->_words = $this->searchableWords($_loadedWords, $maxSearchNum);
    }
 
    // unset the class words variable.
    public function __destruct()
    {
	unset($this->_words);
    }
 
    // load in the words file 
    public function loadWords($filename)
    {
	$xmlFileLoad = simplexml_load_file($filename);
	$returnWords = array();
	$i=0;
	foreach ($xmlFileLoad->word as $theword)
	{
	  // only the string, because otherwise it would be a simpleXMLObject
	  $returnWords[$i++] = (string)$theword;
	}
	return $returnWords;
    }
 
    // searchableWords will create a array of words to search for from the searchablewords parameter, with maximum number of searchs (maxsearch)
    public function searchableWords($searchableWords, $maxSearch)
    {
	$returnSearchWords = array();
	$numberSearchWords = count($searchableWords);
	// if the maxsearch value is greater or equal to the number of searchable words just fill in the return will all of the words
	if ($numberSearchWords <= $maxSearch)
	{
	    for ($i = 0; $i < $numberSearchWords; $i++)
	    {
	      $returnSearchWords[$i] = $searchableWords[$i];
	    }
	}
	else
	{
	  // randomly pick out words from the array of searchable words
	  for ($i = 0; $i < $maxSearch; $i++)
	  {
	    // re-index the array
	    $searchableWords = array_values($searchableWords);
	    // obtain a random index value
	    $searchWordNum = rand(0, count($searchableWords)-1);
	    // insert into the return value
	    $returnSearchWords[$i] = $searchableWords[$searchWordNum];
	    // delete the word from the array
	    unset($searchableWords[$searchWordNum]);
	  }
	}
	return $returnSearchWords;
    }
 
    public function wordsArray()
    {
	return $this->_words;
    }
 
    public function printOutWords()
    {
	prit_r($this->_words);
    }
};
 
 
$wordsToSearch = new Word("words.xml",3);
$wordsToSearch->printOutWords();
?>

and the output would be something like, of course since it is random generated, then could be the same words or different ones!!.

Array ( [0] => was [1] => sole [2] => old )

Leave a Reply

Your email address will not be published. Required fields are marked *