[PHP] Fulltext vyhľadávanie prosím o pomoc

Programovacie jazyky, rady, poradňa...
Majo053
Medium Expert
Medium Expert
Používateľov profilový obrázok
Príspevky: 126
Registrovaný: 14 aug 2006, 13:39
Bydlisko: Somewhere

[PHP] Fulltext vyhľadávanie prosím o pomoc

Príspevok od používateľa Majo053 »

Ahojte velmi by som potreboval upravit nasledujuci skript tak, aby mi vyhladaval nasledovne:

Ak zadam hladany vyraz "vseobecne pravo na slovensku" tak mi tento skript zobrazi vysledok ale ak zadam "vseobecne pravo v rakusku" teda vynecham "na slovensku" a dam "v rakusku" co nemam na stranke tak mi uz nezobrazi hladany vyraz aj ked ho mam na stranke (to "vseobecne pravo na slovensku"). Chcem aby nezavisle hladal slova a nie slovo za slovom a pokial nenajde suvis s prvym a druhym tak sa skonci :? . Potreboval by som fulltext vyhladavanie. Da sa to nejak spravit?

Dakujem pekne ak by sa niekto nasiel :)

Kód: Vybrať všetko

<?
header ('Content-type: text/html; charset=utf-8');
// English Configuration
$my_server = "http://".getenv("SERVER_NAME").":".getenv("SERVER_PORT"); // Your Server (generally no changes needed)
$my_root = getenv("DOCUMENT_ROOT"); // Your document root (generally no changes needed)
$s_dirs = array("/kategorie/ekonomika/zoznam/dane","/kategorie/ekonomika/zoznam/podnik","/kategorie/ekonomika/zoznam/financie","/kategorie/ekonomika/zoznam/marketing","/kategorie/ekonomika/zoznam/manazment","/kategorie/ekonomika/zoznam/matematika","/kategorie/ekonomika/zoznam/uctovnictvo","/kategorie/ekonomika/zoznam/makroekonomia","/kategorie/ekonomika/zoznam/vseobecne","/kategorie/pravo/zoznam/obchodne","/kategorie/pravo/zoznam/obcianske","/kategorie/pravo/zoznam/medzinarodne-a-statne","/kategorie/pravo/zoznam/pracovne","/kategorie/pravo/zoznam/trestne","/kategorie/pravo/zoznam/zbierky","/kategorie/pravo/zoznam/vseobecne"); // Which directories should be searched ("/dir1","/dir2","/dir1/subdir2","/Verzeichniss2/Unterverzeichniss2")? --> $s_dirs = array(""); searches the entire server
$s_skip = array("..",".","subdir2"); // Which files/dirs do you like to skip?
$s_files = "html|htm|HTM|HTML|php3|php4|php|txt"; // Which files types should be searched? Example: "html$|htm$|php4$"
$min_chars = "3"; // Min. chars that must be entered to perform the search
$max_chars = "40"; // Max. chars that can be submited to perform the search
$default_val = "zadajte názov titulu"; // Default value in searchfield
$limit_hits = array("15","20","25","30"); // How many hits should be displayed, to suppress the select-menu simply use one value in the array --> array("100")
$message_1 = "<font color=\"red\">Krátky počet znakov!"; // Invalid searchterm
$message_2 = "Prosím zadajte najmenej '$min_chars' a najviac '$max_chars' znakov."; // Invalid searchterm long ($min_chars/$max_chars)
$message_3 = "Výsledok vyhľadávania pre výraz:"; // Headline searchresults
$message_4 = "Prepáčte, v našej databáze sa nenašli žiadne výsledky <img src=\"/img/sad.gif\">. Ste si istý, že ste zadali celý názov a  s <b>diakritikou</b>? <br><br><br> <b><u>Skúste zadať:</u></b> <br><br> - čo najvýstižnejší výraz (2-3 slovný) <br> - názov knihy s <b>diakritikou</b>! <br>- meno autora <br> - ISBN číslo danej knihy<br> - katalógové číslo danej knihy"; // No hits
$message_5 = "výsledkov"; // Hits
$message_6 = "Match case"; // Match case
$no_title = "Untiteled"; // This should be displayed if no title or empty title is found in file
$limit_extracts_extracts = ""; // How many extratcts per file do you like to display. Default: "" --> every extract, alternative: 'integer' e.g. "3"
$byte_size = "75200"; // How many bytes per file should be searched? Reduce to increase speed


// search_form(): Gibt das Suchformular aus
function search_form($HTTP_GET_VARS, $limit_hits, $default_val, $message_5, $message_6, $PHP_SELF) {
	@$keyword=$HTTP_GET_VARS['keyword'];
	@$case=$HTTP_GET_VARS['case'];
	@$limit=$HTTP_GET_VARS['limit'];
	echo
	"<form action=\"$PHP_SELF\" method=\"GET\" target=\"main\">\n",
	"<input type=\"hidden\" value=\"SEARCH\" name=\"action\">\n",
	"<input type=\"text\" name=\"keyword\" class=\"text\" size=\"10\"  maxlength=\"30\" value=\"";
	if(!$keyword)
		echo "$default_val";
	else
		echo str_replace("&","&",htmlentities($keyword));
	echo "\" ";
	echo "onFocus=\" if (value == '";
	if(!$keyword)
		echo "$default_val"; 
	else
		echo str_replace("&","&",htmlentities($keyword));
	echo "') {value=''}\" onBlur=\"if (value == '') {value='";
	if(!$keyword)
		echo "$default_val"; 
	else
		echo str_replace("&","&",htmlentities($keyword));
	echo "'}\"> ";
	$j=count($limit_hits);
	if($j==1)
		echo "<input type=\"hidden\" value=\"".$limit_hits[0]."\" name=\"limit\">";
	elseif($j>1) {
		echo
		"<select name=\"limit\" class=\"select\">\n";
		for($i=0;$i<$j;$i++) {
			echo "<option value=\"".$limit_hits[$i]."\"";
			if($limit==$limit_hits[$i])
				echo "SELECTED";
			echo ">".$limit_hits[$i]." $message_5</option>\n";
			}
		echo "</select> ";
		}
	echo
	"<input type=\"submit\" value=\"Hľadaj\">\n",
	"</form>\n";
	}


// search_headline(): Ueberschrift Suchergebnisse
function search_headline($HTTP_GET_VARS, $message_3) {
	@$keyword=$HTTP_GET_VARS['keyword'];
	@$action=$HTTP_GET_VARS['action'];
	if($action == "SEARCH") // Volltextsuche
	echo "<h1 class=\"result\">$message_3 '<i>".htmlentities(stripslashes($keyword))."</i>'</h1>";
	}


// search_error(): Auf Fehler testen und Suchfehler anzeigen
function search_error($HTTP_GET_VARS, $min_chars, $max_chars, $message_1, $message_2, $limit_hits) {
	global $HTTP_GET_VARS;
	@$keyword=$HTTP_GET_VARS['keyword'];
	@$action=$HTTP_GET_VARS['action'];
	@$limit=$HTTP_GET_VARS['limit'];
	if($action == "SEARCH") { // Volltextsuche
		if(strlen($keyword)<$min_chars||strlen($keyword)>$max_chars||!in_array ($limit, $limit_hits)) { // Ist die Anfrage in Ordnung (min. '$min_chars' Zeichen, max. '$max_chars' Zeichen)?
			echo "<p class=\"result\"><b>$message_1</b><br>$message_2</p>";
			$HTTP_GET_VARS['action'] = "ERROR"; // Suche abbrechen
			}
		}
	}


// search_dir(): Volltextsuche in Verzeichnissen
function search_dir($my_server, $my_root, $s_dirs, $s_files, $s_skip, $message_1, $message_2, $no_title, $limit_extracts, $byte_size, $HTTP_GET_VARS) {
	global $count_hits;
	@$keyword=$HTTP_GET_VARS['keyword'];
	@$action=$HTTP_GET_VARS['action'];
	@$limit=$HTTP_GET_VARS['limit'];
	@$case=$HTTP_GET_VARS['case'];
	if($action == "SEARCH") { // Volltextsuche
		foreach($s_dirs as $dir) { // Alle Verzeichnisse in $s_dirs durchsuchen
			$handle = @opendir($my_root.$dir);
			while($file = @readdir($handle)) {
				if(in_array($file, $s_skip)) { // Alles in $skip auslassen
					continue;
					}
				elseif($count_hits>=$limit) {
					break; // Maximale Trefferzahl erreicht
					}
				elseif(is_dir($my_root.$dir."/".$file)) { // Unterverzeichnisse durchsuchen
					$s_dirs = array("$dir/$file");
					search_dir($my_server, $my_root, $s_dirs, $s_files, $s_skip, $message_1, $message_2, $no_title, $limit_extracts, $byte_size, $HTTP_GET_VARS); // search_dir() rekursiv auf alle Unterverzeichnisse aufrufen
					}
				elseif(preg_match("/($s_files)$/i", $file)) { // Alle Dateien gemaess Endungen $s_files
					$fd=fopen($my_root.$dir."/".$file,"r");
					$text=fread($fd, $byte_size); // 50 KB
					$keyword_html = htmlentities($keyword);
					if($case) { // Gross-/Kleinschreibung beruecksichtigen?
						$do=strstr($text, $keyword)||strstr($text, $keyword_html);
						}
					else {
						$do=stristr($text, $keyword)||stristr($text, $keyword_html);
						}
					if($do)	{
						$count_hits++; // Treffer zaehlen
						if(preg_match_all("=<title[^>]*>(.*)</title>=siU", $text, $titel)) { // Generierung des Link-Textets aus <title>...</title>
							if(!$titel[1][0]) // <title></title> ist leer...
								$link_title=$no_title; // ...also $no_title
							else
								$link_title=$titel[1][0];  // <title>...</title> vorhanden...
							}
						else {
							$link_title=$no_title; // ...ansonsten $no_title
							}
						echo "<a href=\"$my_server$dir/$file\" target=\"_self\" class=\"result\">$count_hits.  $link_title</a><br>"; // Ausgabe des Links
						$auszug = strip_tags($text);
						$keyword = preg_quote($keyword); // unescapen
						$keyword = str_replace("/","\/","$keyword");
						$keyword_html = preg_quote($keyword_html); // unescapen
						$keyword_html = str_replace("/","\/","$keyword_html");
						echo "<span class=\"extract\">";
						if(preg_match_all("/((\s\S*){0,3})($keyword|$keyword_html)((\s?\S*){0,3})/i", $auszug, $match, PREG_SET_ORDER)); {
							if(!$limit_extracts)
								$number=count($match);
							else
								$number=$limit_extracts;
							for ($h=0;$h<$number;$h++) { // Kein Limit angegeben also alle Vorkommen ausgeben
								if (!empty($match[$h][3]))
									printf("<i><b>..</b> %s<b>%s</b>%s <b>..</b></i>", $match[$h][1], $match[$h][3], $match[$h][4]);
								}
							}
						echo "</span><br><br>";
						flush();
						}
					fclose($fd);
					}
				}
	  		@closedir($handle);
			}
		}
	}


// search_no_hits(): Ausgabe 'keine Treffer' bei der Suche
function search_no_hits($HTTP_GET_VARS, $count_hits, $message_4) {
	@$action=$HTTP_GET_VARS['action'];
	if($action == "SEARCH" && $count_hits<1) // Volltextsuche, kein Treffer
		echo "<p class=\"result\">$message_4</p>";
	}

?>
DomCZo
Light Star
Light Star
Používateľov profilový obrázok
Príspevky: 235
Registrovaný: 16 okt 2006, 22:49

Príspevok od používateľa DomCZo »

Tvoj zdrojak si ani nepozeram, lebo je to bordel. Dufam, ze tento kod pomoze:

Kód: Vybrať všetko

<?php


$klucoveslova = "prve druhe tretie";
$retazec1 = "Za prve co som urobil, bolo, ze som bla bla a potom to bolo.";  // ANO
$retazec2 = "Za to co som urobil, bolo, ze som bla bla druhe a potom to bolo.";  // ANO
$retazec3 = "Za to co som urobil, bolo, ze sa to tu nenajde.";  // NIE

#### ---------- ####
function FulltextHladaj($co, $kde)
{
  $co_casti = explode(" ", $co);
  while(list($poradie, $slovo) = each($co_casti))
  {
    if(eregi($slovo, $kde))
    {
      return true;
      exit;
    }
  }
}
#### ---------- ####

if(FulltextHladaj($klucoveslova, $retazec1))  // vypise: ANO
{
  echo "ano<br>\n";
}
else
{
  echo "nie<br>\n";
}

if(FulltextHladaj($klucoveslova, $retazec2))  // vypise: ANO
{
  echo "ano<br>\n";
}
else
{
  echo "nie<br>\n";
}

if(FulltextHladaj($klucoveslova, $retazec3))// vypise: NIE
{
  echo "ano<br>\n";
}
else
{
  echo "nie<br>\n";
}


#### ---------- ####
function FulltextHladajVSubore($co, $subor)
{
  $co_casti = explode(" ", $co);
  $subor_casti = file($subor);
  for($i = 0; $i <= count($co_casti)-1; $i++)
  {
    for($j = 0; $j <= count($subor_casti)-1; $j++)
    {
      if(eregi($co_casti[$i], $subor_casti[$j]))
      {
        return true;
        exit;
      }
    }
  }
}
#### ---------- ####

if(FulltextHladajVSubore("daco qdqqw asdf dfew label", "aa2.php"))
{
  echo "ano<br>\n";
}
else
{
  echo "nie<br>\n";
}


?>
Majo053
Medium Expert
Medium Expert
Používateľov profilový obrázok
Príspevky: 126
Registrovaný: 14 aug 2006, 13:39
Bydlisko: Somewhere

Príspevok od používateľa Majo053 »

Dakujem pekne ale ja to potrebujem dat do toho skriptu.. Mne to nefunguje...

V tejto casti kodu by si mi to mohol upravit? Tu by to trebalo tak aby mi vypisal vsetky mozne vysledky vzhladom na vsetky zadavane vyrazy.

Kód: Vybrať všetko

if($action == "SEARCH") { // Volltextsuche
		foreach($s_dirs as $dir) { // Alle Verzeichnisse in $s_dirs durchsuchen
			$handle = @opendir($my_root.$dir);
			while($file = @readdir($handle)) {
				if(in_array($file, $s_skip)) { // Alles in $skip auslassen
					continue;
					}
				elseif($count_hits>=$limit) {
					break; // Maximale Trefferzahl erreicht
					}
				elseif(is_dir($my_root.$dir."/".$file)) { // Unterverzeichnisse durchsuchen
					$s_dirs = array("$dir/$file");
					search_dir($my_server, $my_root, $s_dirs, $s_files, $s_skip, $message_1, $message_2, $no_title, $limit_extracts, $byte_size, $HTTP_GET_VARS); // search_dir() rekursiv auf alle Unterverzeichnisse aufrufen
					}
				elseif(preg_match("/($s_files)$/i", $file)) { // Alle Dateien gemaess Endungen $s_files
					$fd=fopen($my_root.$dir."/".$file,"r");
					$text=fread($fd, $byte_size); // 50 KB
					$keyword_html = htmlentities($keyword);
					if($case) { // Gross-/Kleinschreibung beruecksichtigen?
						$do=strstr($text, $keyword)||strstr($text, $keyword_html);
						}
					else {
						$do=stristr($text, $keyword)||stristr($text, $keyword_html);
						}
					if($do)	{
						$count_hits++; // Treffer zaehlen
						if(preg_match_all("=<title[^>]*>(.*)</title>=siU", $text, $titel)) { // Generierung des Link-Textets aus <title>...</title>
							if(!$titel[1][0]) // <title></title> ist leer...
								$link_title=$no_title; // ...also $no_title
							else
								$link_title=$titel[1][0];  // <title>...</title> vorhanden...
							}
						else {
							$link_title=$no_title; // ...ansonsten $no_title
							}
						echo "<a href=\"$my_server$dir/$file\" target=\"_self\" class=\"result\">$count_hits.  $link_title</a><br>"; // Ausgabe des Links
						$auszug = strip_tags($text);
						$keyword = preg_quote($keyword); // unescapen
						$keyword = str_replace("/","\/","$keyword");
						$keyword_html = preg_quote($keyword_html); // unescapen
						$keyword_html = str_replace("/","\/","$keyword_html");
						echo "<span class=\"extract\">";
						if(preg_match_all("/((\s\S*){0,3})($keyword|$keyword_html)((\s?\S*){0,3})/i", $auszug, $match, PREG_SET_ORDER)); {
							if(!$limit_extracts)
								$number=count($match);
							else
								$number=$limit_extracts;
							for ($h=0;$h<$number;$h++) { // Kein Limit angegeben also alle Vorkommen ausgeben
								if (!empty($match[$h][3]))
									printf("<i><b>..</b> %s<b>%s</b>%s <b>..</b></i>", $match[$h][1], $match[$h][3], $match[$h][4]);
								}
							}
						echo "</span><br><br>";
						flush();
						}
					fclose($fd);
					}
				}
	  		@closedir($handle);
			}
		}
	}
DomCZo
Light Star
Light Star
Používateľov profilový obrázok
Príspevky: 235
Registrovaný: 16 okt 2006, 22:49

Príspevok od používateľa DomCZo »

No mozno ti niekto helpne, pre mna je to bordel a nemam hodiny cas studovat to :)
Majo053
Medium Expert
Medium Expert
Používateľov profilový obrázok
Príspevky: 126
Registrovaný: 14 aug 2006, 13:39
Bydlisko: Somewhere

Príspevok od používateľa Majo053 »

DomCZo napísal:No mozno ti niekto helpne, pre mna je to bordel a nemam hodiny cas studovat to :)
fajn v pohode ak by si mal cas niekedy tak mi mozes pomoct :oops:
Napísať odpoveď