[QUIZ#1] Denfie (PHP)

Denfie

Mitglied
PHP:
<?php
$in = "";
$after_body = "";
if(isset($_POST['search_str'])){
	$in 		= $_POST['search_str'];
	
	class search_algo {
		private $in_arr  	 = array();
		private $out_arr  	 = array();
		private $str_arr	 = array();
		private $search_mask = ".*";
		
		public function __construct(){
			
		}// end function __construct
		
		//########################## public functions ################################################
		/**
		 * 	Made an array with all founded names
		 *	
		 *	@param string $str 
		 *
		 *	@author Dennis Fiedler (dp_logic)
		 */
		public function getPregSearch($str){
			$str = str_replace(" ", "", $str);
			if(!empty($str)){
				$str_arr = str_split($str);
				$this->str_arr = $str_arr;
				$this->search_mask .= join(".*", $str_arr).".*";
			}// end if
//			print("Search-Mask: `/".$this->search_mask."/s`");
			$this->out_arr = array();
			foreach($this->in_arr as $value){
				if(preg_match("/".$this->search_mask."/s", $value)){
					array_push($this->out_arr, $value);
				}// end if
			}// end for
		}// end function getSearch
		
		public function getMySearchEngine($str){
			$str = str_replace(" ", "", $str);
			if(!empty($str)){
				$this->out_arr = array();
				$str_arr = str_split($str);
				$this->str_arr = $str_arr;
				foreach($this->in_arr as $value){
					$cnt = 0;
					if(strlen($value) < count($str_arr))
						continue;
					for($i = 0; $i < strlen($value); $i++){
						if($value[$i] == $str_arr[$cnt]){
							$cnt++;
							if($cnt >= count($str_arr)){
								array_push($this->out_arr, $value);
								break;
							}// end if
						}// end if
					}// end for
				}// end for
			}else{
				foreach($this->in_arr as $value){
					array_push($this->out_arr, $value);
				}// end foreach
			}// end if else
		}// end function getMySearchEngine
		
		public function getList(){
			$this->prepareList();
			return $this->out_arr;
		}// end function getList
		
		public function setSource(&$handle){
			$fp = "";
			if(is_string($handle) && is_file($handle)){
				$fp = fopen($handle, "r");
			}else if(is_uploaded_file($handle["tmp_name"])){
				$fp = fopen($handle["tmp_name"], "r");
			}else{
				user_error("[Function <<b>".__FUNCTION__."</b>> has problems with handle]<br/>" , E_USER_ERROR);
				return;
			}// end if else
			
			if ($fp){
				while(!feof($fp)){
					$text = fgets($fp);
					array_push($this->in_arr, $text);
				}// end while
				fclose($fp);
			}// end if
		}// end function setSource
		
		public function modList(){
			for($i = 0; $i < count($this->out_arr); $i++){
				$cnt = 0;
				$str = "";
				$check = false;
				$this->out_arr[$i] = trim($this->out_arr[$i]);
				for($j = 0; $j < strlen($this->out_arr[$i]); $j++){
					if($this->out_arr[$i][$j] === $this->str_arr[$cnt]){
						if($check){
							$str .= $this->out_arr[$i][$j];
						}else{
							$str .= "<".$this->out_arr[$i][$j];
							$check = true;
						}// end if else
						$cnt++;	
					}else{
						if($check){
							$str .= ">".$this->out_arr[$i][$j];
							$check=false;
						}else{
							$str .= $this->out_arr[$i][$j];
						}// end if else
					}// end if else
				}// end for
				if($check)
					$str .= ">";
				$this->out_arr[$i] = $str;
			}// end for
		}// end function modList

		//########################## private functions ###############################################
		private function prepareList(){
			for($i = 0; $i < count($this->out_arr); $i++){
				$this->out_arr[$i] = htmlspecialchars($this->out_arr[$i]);
			}// end for
		}// end function prepareList
	}// end class
	
	$fp;
	$search = new search_algo();
	if(is_uploaded_file($_FILES["upfile"]["tmp_name"])){
		$search->setSource($_FILES["upfile"]);
	}else{
		$string = "presidents-1.txt";
		if(is_file($string)){
			$search->setSource($string);
		}else{
			print("<h3>Error `<i>upfile</i>` is not a file pls chose another file or put in the same folder a file names `presidents-1.txt`.</h3>");
		}// end if else
	}// end if else
//	$search->getPregSearch($in); // Search mit pregMatch
	$search->getMySearchEngine($in); // Search mit eigener Engine
	$search->modList(); // erweiterte Ausgabe
	$list = $search->getList();
	if (is_array($list) and !empty($list) ){
		$after_body = "<ul>\n";
		foreach($list as $value){
			$after_body .= "<li>".$value."</li>\n";
		}// end foreach
		$after_body .= "</ul>\n";
	}else{
		print("<h3>Error `<i>list</i>` is not an array or is empty. <br/>String `".$in."` not found.</h3>");
	}// end if else
}

$body  = "<html><head><title>Search engine</title></head><body>";
$body .= "<form method='post' enctype='multipart/form-data' action='".$_SERVER['PHP_SELF']."' name='search_form' onSubmit='return form_check()'>\n";
$body .= "<input type='file' name='upfile' id='upfile' title='File upload'/></br>";
$body .= "<input type='text' name='search_str' id='search_str' title='Search string' value='".$in."' />";
$body .= "<input type='submit' name='submit' id='submit' title='Submit' value='Search' />";
$body .= "</form>";
$body .= $after_body;
$body .= "</body></html>";
print($body);
?>
 
Zuletzt bearbeitet:

Raubkopierer

Erfahrenes Mitglied
Solange man einen Apache lokal aufm PC hat. Da finde ich Perl und Python komfortabler, da die sowieso jeder Linuxer per default aufm Rechner hat. Aber PHP ist natürlich auch einer von vielen Wegen.