<?php
	function sql_select($post_name, $mysql_name, $sql_select, $typ) {
		if ($post_name != "") {
			// trimmen und überflüssige Leerzeichen entfernen
			$post_name = trim(preg_replace('/\s+/', ' ', $post_name));
			$arr_post_name = explode(" ", $post_name);
			$l_e = count($arr_post_name)-1;
			// Wenn Anzahl der Wörter ungerade und "AND" vorkommt, aber nicht am Anfang od. Ende
			if ( ((count($arr_post_name)%2) != 0 ) AND ( in_array("AND", $arr_post_name) == true) AND ($arr_post_name[0] != "AND") AND ($arr_post_name[$l_e] != "AND") ) {
				// Anz. der "AND" zaehlen
				$anz_el = count($arr_post_name);
				$i = 0;
				while ($i < $anz_el) {
					if ($typ == "T") {
						$arr_post_name [$i] = $mysql_name .  " LIKE '%" . $arr_post_name[$i] . "%'";
					} else {
						$arr_post_name[$i] = "((" . $mysql_name . ".Name LIKE '%" . $arr_post_name[$i] .  "%') OR (" . $mysql_name . ".Originalname LIKE '%" . $arr_post_name[$i] . "%'))";
					}
					$i = $i +2;
				}
				$sql_select =  $sql_select . " AND " . implode(" ", $arr_post_name);
			} else {
				if ($typ == "T") {
					$sql_select = $sql_select . " AND " . $mysql_name . " LIKE '%$post_name%'";
				} else {
					$sql_select = $sql_select . " AND ((". $mysql_name . ".Name LIKE '%" . $post_name. "%') OR (" . $mysql_name . ".Originalname LIKE '%" . $post_name. "%'))";
				}
			}
		}
		return $sql_select;
	}
?>