-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetRandomText.php
132 lines (125 loc) · 3.67 KB
/
GetRandomText.php
1
<?php// GetRandomText 1.1 by Nate Baldwin, www.mindpalette.com (freeware - use at your own risk) 2006/08/21.//-------------------------------------------------------------------------------------------------// MAIN TEXT IMPORT AND RANDOMIZER CLASS//-------------------------------------------------------------------------------------------------class MPRandomText { var $filepath; var $sepstring; var $fixchar; var $textfix; var $contents; var $random; var $errors; // initiate object and start functions function MPRandomText($filepath, $sepstring, $textfix) { $this->filepath = $filepath; $this->textfix = $textfix; $this->sepstring = $sepstring; $this->errors = ""; $this->contents = ""; $this->FileToString(); } // read file contents into string variable function FileToString() { if (!$this->filepath || !file_exists($this->filepath)) $this->errors = "Could not find text file at ".$this->filepath; else { @$filePointer = fopen($this->filepath, "r"); if (!$filePointer) $this->errors = "Text file could not be opened."; if ($this->errors == "") { $this->contents = fread($filePointer, filesize($this->filepath)); fclose($filePointer); if (!$this->textfix) $this->HTMLContent(); $this->MakeArray(); } } } // if importing an HTML page, drop everything outside of (and including) the <body> tags function HTMLContent() { $test = stristr($this->contents, "<body"); if ($test !== false) { $test = stristr($test, ">"); if ($test !== false) { $test = str_replace("</BODY>", "</body>", $test); $test = explode("</body>", substr($test, 1)); if (count($test) > 1) $this->contents = $test[0]; } } } // convert the file text into a list using separation character function MakeArray() { $array = explode($this->sepstring, $this->contents); if (count($array) > 0) { $this->contents = $array; $this->CleanTextList(); } else $this->errors = "Text file contents are empty or could not be read."; } // clean up the list of empty values and extra white space function CleanTextList() { $result = array(); if (is_array($this->contents)) { for ($n=0; $n<count($this->contents); $n++) { $string = trim($this->contents[$n]); $test = trim($string."test"); if (!empty($string) AND $test != "test") $result[] = $string; } if (count($result) > 0) { $this->contents = $result; $this->RandomString(); } } } // get random text string from list function RandomString() { reset($this->contents); srand((double) microtime() * 1000000); shuffle($this->contents); $this->random = $this->contents[0]; } // send finished results to be printed into HTML page function GetResults() { if ($this->errors != "") return $this->errors; else { if ($this->textfix == true) $this->random = htmlentities($this->random); return $this->random; } } }//-------------------------------------------------------------------------------------------------// FUNCTION AND VARIABLE TO CREATE RANDOM TEXT INSTANCE//-------------------------------------------------------------------------------------------------// create variable to store instance references$MPRandomTextHandles = array();// function to create new handle and import random textfunction MPPrintRandomText($MPTextFile = "random.txt", $MPSepString = "*divider*", $MPTextToHTML = false) { global $MPRandomTextHandles; for ($n=0; $n<250; $n++) { if (!isset($MPRandomTextHandles[$n])) { $MPRandomTextHandles[$n] = new MPRandomText($MPTextFile, $MPSepString, $MPTextToHTML); break; } } return ($MPRandomTextHandles[$n]->GetResults()); //return $MPRandomTextHandles[$n]; }?>