Code of adrian
<?php
/*********************************************************
* Author: Adrian Zenteno
* Date : June 12, 2003
* File : functions.php
* Desc : Class to find shuffled word(s) in a sentence.
********************************************************/
class WORDS
{
var $strListWords;
var $numTotalLetters;
var $strNameFile;
//constructor class
function WORDS($strNameFile='')
{
$this->strListWords = '';
$this->intTotalLetters = 0;
$this->strNameFile = $strNameFile;
}
function __construct($strNameFile='')
{
$this->WORDS($strNameFile);
}
//Auxiliar functions
static function addArray(&$array, $key, $val) { $tempArray = array("$key" => "$val"); $array = array_merge ($array, $tempArray); }
function addListWords($strListWords='') { $this->strListWords.=$strListWords; }
function addTotalLetters($intNumLetters=0) { $this->intTotalLetters+=$intNumLetters; }
function getListWords() { return $this->strListWords; }
function getTotalLetters() { return $this->intTotalLetters; }
function getNameFile() { return $this->strNameFile; }
function assignNameFile($strNameFile='') { $this->strNameFile = $strNameFile; }
//function to read file
function WORDSFILE()
{
$arrayFile = array();
if($strNameFile = WORDS::getNameFile()) {
if(file_exists($strNameFile)) {
$tmpListSentence = array();
$fp = fopen("$strNameFile", "r");
$strFile= fread($fp, filesize("$strNameFile"));
fclose($fp);
if($strFile) {
$strFile=preg_replace("/[\.]/","",$strFile);
$arrayTmpFile = split("[\n]",$strFile);
foreach($arrayTmpFile as $tmpLine) {
$tmpLine = trim($tmpLine);
if(substr(trim($tmpLine), -1)==",") $tmpList = preg_replace("/[\,]/","",$tmpLine);
else if($tmpList&&$tmpLine) array_push($tmpListSentence, "$tmpList|$tmpLine");
}
}
} else {$this->addListWords("file $strNameFile does not exist<br>");}
}
if(is_array($tmpListSentence))
foreach($tmpListSentence as $listTmp) {
list($tmpList,$tmpSentence) = split("[\|]",$listTmp);
WORDS::FINDWORDS($tmpList,$tmpSentence);
}
}
//main function
function FINDWORDS($listWords='', $listSentences='')
{
$arrayLetters = array();$arrayBlanks = array();
$arrayTmpBlanks = array();$arrayTmpLetters = array();
$arrayTmpWordsPerList = array();$strListWords = '';
$flgAsigToLetters = FALSE;$strTmpWord = '';
if(((strlen($listWords)>=20)&&(strlen($listWords)<=80)) && (strlen($listSentences)>0)) {
$listSentences = str_replace(" ",'',strtolower($listSentences));
$result = (count_chars($listSentences, 0));
for ($i=97; $i<=122; $i++) WORDS::addArray($arrayLetters, chr($i), $result[$i]);
$arrayWords = split(" ", $listWords);
foreach($arrayWords as $word) {//take each word
$arrayTmpLetters = $arrayLetters;
$arrayTmpBlanks = $arrayBlanks;
$lengthWord = strlen($word);$cont=0;$flgIgnoreWord=FALSE;
while(($cont<$lengthWord) && !$flgIgnoreWord) {//take letters, one by one
$letter = $word{$cont};
$tmpValue = $arrayTmpLetters["$letter"];
if(($tmpValue>0)&&($numBlanks<2)&&!$flgIgnoreWord) {
$tmpValue--;
$arrayTmpLetters["$letter"] = $tmpValue;
$flgAsigToLetters = TRUE;
$strTmpWord.=$letter;
}
else {
$numBlanks = count($arrayTmpBlanks);
if(($numBlanks<2)) {
if(array_key_exists("$letter", $arrayTmpBlanks))
{$flgAsigToLetters = FALSE;$flgIgnoreWord=TRUE;}
else {
WORDS::addArray($arrayTmpBlanks, $letter, 1);
$strTmpWord.='<font color=FF0000>'.$letter.'</font>';
$flgAsigToLetters = TRUE;
}
}
else $flgAsigToLetters = FALSE;
}$cont++;
}
if($flgAsigToLetters) {
$arrayLetters = $arrayTmpLetters;
$arrayBlanks = $arrayTmpBlanks;
WORDS::addTotalLetters($lengthWord);
WORDS::addArray($arrayTmpWordsPerList,$strTmpWord,$lengthWord);
}
$strTmpWord = '';$flgAsigToLetters = FALSE;$numBlanks = 0;
}
array_multisort($arrayTmpWordsPerList,SORT_DESC,array_keys($arrayTmpWordsPerList));
if(count($arrayTmpWordsPerList)>0)
foreach($arrayTmpWordsPerList as $tmpKey=>$tmpWord) $strListWords.= $tmpKey.' ';
}
WORDS::addListWords($strListWords.'<br>');
}
}
// *************************************************
// php-editors note - below was in a separate file -
// calling the above as an include
// *************************************************
$objWords = new WORDS('lists.txt');
$objWords->WORDSFILE();
echo($objWords->GETLISTWORDS());
echo($objWords->GETTOTALLETTERS());
?>
Back to results
|
|