{"id":444,"date":"2010-01-05T12:05:39","date_gmt":"2010-01-05T12:05:39","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=444"},"modified":"2010-01-07T10:20:35","modified_gmt":"2010-01-07T10:20:35","slug":"wordsearch-php-words-to-search-for","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/01\/05\/wordsearch-php-words-to-search-for\/","title":{"rendered":"Wordsearch php &#8211; Words to search for"},"content":{"rendered":"<p>As from the main project, <a href=\"http:\/\/www.codingfriends.com\/index.php\/projects\/word-search\">wordsearch<\/a>, 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 <a href=\"http:\/\/www.codingfriends.com\/index.php\/2010\/01\/04\/wordsearch-php-grid-class\/\">here<\/a>.<\/p>\n<p>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. <\/p>\n<p>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.<\/p>\n<p>I have included comments in the code, the <strong>__construct <\/strong> and <strong>__destruct<\/strong> are what happens when the class is created (via the &#8220;new&#8221; command) and destroyed respectively.  Just a nice way to create the class\/object and also clean up after itself.<\/p>\n<p>The parameters that are passed to the <strong>__construct<\/strong> 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.<\/p>\n<pre lang=\"php\">\r\n<?php\r\n\r\nclass Word {\r\n    \/* private section of the class word *\/\r\n    \r\n    private $_words;\r\n    \r\n    \/* constructor for the word class \r\n       default action to load a xml file to gain 5 words\r\n    *\/\r\n    \r\n    public function __construct($wordsOrFilename = \"words.xml\", $maxSearchNum = 5)\r\n    {\r\n\t$this->_words = array();\r\n\t\r\n\t\/\/ if the filename is actually words from the input screen then just add in them instead\r\n\tif (is_array($wordsOrFilename)) \r\n\t{\r\n\t  $_loadedWords = $wordsOrFilename;\r\n\t}\r\n\telse\r\n\t{\r\n\t  \/\/ load the file of words into a array\r\n\t  $_loadedWords = $this->loadWords($wordsOrFilename);\r\n\t}\r\n\t\/\/ create a array of words to be searched for, max number as above\r\n\t$this->_words = $this->searchableWords($_loadedWords, $maxSearchNum);\r\n    }\r\n    \r\n    \/\/ unset the class words variable.\r\n    public function __destruct()\r\n    {\r\n\tunset($this->_words);\r\n    }\r\n\r\n    \/\/ load in the words file \r\n    public function loadWords($filename)\r\n    {\r\n\t$xmlFileLoad = simplexml_load_file($filename);\r\n\t$returnWords = array();\r\n\t$i=0;\r\n\tforeach ($xmlFileLoad->word as $theword)\r\n\t{\r\n\t  \/\/ only the string, because otherwise it would be a simpleXMLObject\r\n\t  $returnWords[$i++] = (string)$theword;\r\n\t}\r\n\treturn $returnWords;\r\n    }\r\n\r\n    \/\/ searchableWords will create a array of words to search for from the searchablewords parameter, with maximum number of searchs (maxsearch)\r\n    public function searchableWords($searchableWords, $maxSearch)\r\n    {\r\n\t$returnSearchWords = array();\r\n\t$numberSearchWords = count($searchableWords);\r\n\t\/\/ if the maxsearch value is greater or equal to the number of searchable words just fill in the return will all of the words\r\n\tif ($numberSearchWords <= $maxSearch)\r\n\t{\r\n\t    for ($i = 0; $i < $numberSearchWords; $i++)\r\n\t    {\r\n\t      $returnSearchWords[$i] = $searchableWords[$i];\r\n\t    }\r\n\t}\r\n\telse\r\n\t{\r\n\t  \/\/ randomly pick out words from the array of searchable words\r\n\t  for ($i = 0; $i < $maxSearch; $i++)\r\n\t  {\r\n\t    \/\/ re-index the array\r\n\t    $searchableWords = array_values($searchableWords);\r\n\t    \/\/ obtain a random index value\r\n\t    $searchWordNum = rand(0, count($searchableWords)-1);\r\n\t    \/\/ insert into the return value\r\n\t    $returnSearchWords[$i] = $searchableWords[$searchWordNum];\r\n\t    \/\/ delete the word from the array\r\n\t    unset($searchableWords[$searchWordNum]);\r\n\t  }\r\n\t}\r\n\treturn $returnSearchWords;\r\n    }\r\n    \r\n    public function wordsArray()\r\n    {\r\n\treturn $this->_words;\r\n    }\r\n\r\n    public function printOutWords()\r\n    {\r\n\tprit_r($this->_words);\r\n    }\r\n};\r\n\r\n\r\n$wordsToSearch = new Word(\"words.xml\",3);\r\n$wordsToSearch->printOutWords();\r\n?>\r\n<\/pre>\n<p>and the output would be something like, of course since it is random generated, then could be the same words or different ones!!.<\/p>\n<pre lang=\"html\">\r\nArray ( [0] => was [1] => sole [2] => old ) \r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/01\/05\/wordsearch-php-words-to-search-for\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Wordsearch php &#8211; Words to search for<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[45],"class_list":["post-444","post","type-post","status-publish","format-standard","hentry","category-php","tag-wordsearch"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/444","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=444"}],"version-history":[{"count":6,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/444\/revisions"}],"predecessor-version":[{"id":462,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/444\/revisions\/462"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=444"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=444"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=444"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}