Hallo,
für die Suchbox meiner Website wurde mir ein PHP Script zur Volltextsuche zur Verfügung gestellt. Leider wird bei der Ausgabe der gefundenen Treffer, alles direkt unter dem Suchfeld unformatiert angezeigt. Könnte mir eventuell jemand helfen wie ich es schaffe die Treffer in meinem Inhaltsbereich auf der Website anzeigen zu lassen?
(Im Anhang befindet sich ein Bild vom momentanen Zustand)
Vielen Dank!
Anbei der PHP-Code:
<?php
$buttonvalue = "Search";
$search_at = "Ihre Suche nach";
$search_result = "ergab diese Resultate:";
$pages = "Seitenzahl mit Treffern: ";
$to_small = "At least two characters is required";
$recursive = true;
$html = <<<HTML
<p><br /></p>
<form name="form" action="" size="40">
<input type="text" name="search" size="30" required placeholder="Suchen..." style="border:1px solid #747579;font-family:Arial;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;"/>
<input type="image" src="searchicon.png" width="30" height="30" value="$buttonvalue"
onclick='window.location.assign(document.URL.subst ring(0,document.URL.indexOf("?")) + "?search=" + document.form.search.value.replace(/ /g,"%20"))' />
</form>
HTML;
echo $html;
function textpart($body, $search) {
$length = 30;
$text = substr($body, max(stripos($body,$search) - $length, 0), strripos($body,$search) - stripos($body,$search) + strlen($search) + 2 * $length);
if (strripos($text, " ") < strripos($text,$search)) {
$text = $text . " ";
}
if (stripos($text, " ") != strripos($text, " ")) {
$text = substr($text, stripos($text, " "), strripos($text, " ") - stripos($text, " "));
}
$temp = $text;
$stop = substr($text, strripos($text, $search) + strlen($search));
if (strlen($stop) > $length) {
$stop = substr($text, strripos($text, $search) + strlen($search), $length);
$stop = substr($stop, 0, strripos($stop, " "));
}
$text = "... ";
while (stripos($temp,$search)) {
$temp = substr_replace($temp, "<b>", stripos($temp, $search), 0);
$temp = substr_replace($temp, "</b>", stripos($temp, $search) + strlen($search), 0);
$text = $text . substr($temp, 0, stripos($temp, "</b>") + 4);
$temp = substr($temp, stripos($temp, "</b>") + 4);
if(stripos($temp, $search) > (2 * $length)) {
$text = $text . substr($temp, 0, $length);
$text = substr($text, 0, strripos($text, " ")) . " ... ";
$temp = substr($temp, stripos($temp, $search) - $length);
$temp = substr($temp, stripos($temp, " "));
}
}
$text = $text . $stop . " ... ";
echo $text;
return;
}
function compress($string, $first, $last) {
while(stripos($string,$first) && stripos($string,$last)) {
$string = substr_replace($string, "", stripos($string,$first), stripos($string,$last) - stripos($string,$first) + strlen($last));
}
return $string;
}
function directoryToArray($directory, $recursive) {
$array_items = array();
if ($handle = opendir($directory)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (is_dir($directory. "/" . $file)) {
if($recursive) {
$array_items = array_merge($array_items, directoryToArray($directory. "/" . $file, $recursive));
}
} else {
$file = $directory . "/" . $file;
$array_items[] = preg_replace("/\/\//si", "/", $file);
}
}
}
closedir($handle);
}
return $array_items;
}
function filewalk($file, $search, $counter, $webpath) {
if (strtolower(substr($file, stripos($file, ".htm"))) == ".htm"
|| strtolower(substr($file, stripos($file, ".html"))) == ".html"
|| strtolower(substr($file, stripos($file, ".asp"))) == ".asp"
|| strtolower(substr($file, stripos($file, ".php"))) == ".php") {
$all = file_get_contents($file);
$body = substr($all, stripos($all,"<body"),stripos($all,"</body>") - stripos($all,"<body"));
$body = preg_replace('/<br \/>/i', ' ', $body);
$body = preg_replace('/<br>/i', ' ', $body);
$body = compress($body,"<noscript","</noscript>");
$body = compress($body,"<script","</script>");
$body = compress($body,"<iframe","</iframe>");
$body = compress($body,"<noframe","</noframe>");
$body = strip_tags($body);
$body = html_entity_decode($body, ENT_QUOTES);
$body = preg_replace('/\s+/', ' ', $body);
if (stripos($body, $search)) {
$title = substr($all, stripos($all,"<title>") + 7,stripos($all,"</title>") - stripos($all,"<title>") - 7);
$title = html_entity_decode($title, ENT_QUOTES);
$title = preg_replace('/\s+/', ' ', $title);
echo '<p><a href="' . $file . '">' . $title . '</a></br>';
echo '<span id="webpath">' . $webpath . substr($file, stripos($file, "/")) . '</span><br />';
echo textpart($body, $search) . '</p>';
$counter = $counter + 1;
}
}
return $counter;
}
$url = $_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://';
$url .= $_SERVER['SERVER_PORT'] != '80' ? $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"] : $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (stripos($url,"?search=")) $search = $_GET['search'];
$webpath = dirname($url);
if (strlen($search) < 2 && trim($search) <> "") {
echo '<p>' . $to_small . '!</p>';
$search = "";
}
if (trim($search) <> "") {
echo "<p>" . $search_at . " '<b>" . $search . "</b>' " . $search_result . ".</p>";
$counter = 0;
$curdir = getcwd();
if ($dir = opendir($curdir)) {
$files = directoryToArray("./", $recursive);
foreach ($files as $file) {
$counter = filewalk($file, $search, $counter, $webpath);
}
closedir($dir);
}
echo "<p>" . $pages . ": " . $counter . "</p>";
}
?>
für die Suchbox meiner Website wurde mir ein PHP Script zur Volltextsuche zur Verfügung gestellt. Leider wird bei der Ausgabe der gefundenen Treffer, alles direkt unter dem Suchfeld unformatiert angezeigt. Könnte mir eventuell jemand helfen wie ich es schaffe die Treffer in meinem Inhaltsbereich auf der Website anzeigen zu lassen?
(Im Anhang befindet sich ein Bild vom momentanen Zustand)
Vielen Dank!
Anbei der PHP-Code:
<?php
$buttonvalue = "Search";
$search_at = "Ihre Suche nach";
$search_result = "ergab diese Resultate:";
$pages = "Seitenzahl mit Treffern: ";
$to_small = "At least two characters is required";
$recursive = true;
$html = <<<HTML
<p><br /></p>
<form name="form" action="" size="40">
<input type="text" name="search" size="30" required placeholder="Suchen..." style="border:1px solid #747579;font-family:Arial;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;"/>
<input type="image" src="searchicon.png" width="30" height="30" value="$buttonvalue"
onclick='window.location.assign(document.URL.subst ring(0,document.URL.indexOf("?")) + "?search=" + document.form.search.value.replace(/ /g,"%20"))' />
</form>
HTML;
echo $html;
function textpart($body, $search) {
$length = 30;
$text = substr($body, max(stripos($body,$search) - $length, 0), strripos($body,$search) - stripos($body,$search) + strlen($search) + 2 * $length);
if (strripos($text, " ") < strripos($text,$search)) {
$text = $text . " ";
}
if (stripos($text, " ") != strripos($text, " ")) {
$text = substr($text, stripos($text, " "), strripos($text, " ") - stripos($text, " "));
}
$temp = $text;
$stop = substr($text, strripos($text, $search) + strlen($search));
if (strlen($stop) > $length) {
$stop = substr($text, strripos($text, $search) + strlen($search), $length);
$stop = substr($stop, 0, strripos($stop, " "));
}
$text = "... ";
while (stripos($temp,$search)) {
$temp = substr_replace($temp, "<b>", stripos($temp, $search), 0);
$temp = substr_replace($temp, "</b>", stripos($temp, $search) + strlen($search), 0);
$text = $text . substr($temp, 0, stripos($temp, "</b>") + 4);
$temp = substr($temp, stripos($temp, "</b>") + 4);
if(stripos($temp, $search) > (2 * $length)) {
$text = $text . substr($temp, 0, $length);
$text = substr($text, 0, strripos($text, " ")) . " ... ";
$temp = substr($temp, stripos($temp, $search) - $length);
$temp = substr($temp, stripos($temp, " "));
}
}
$text = $text . $stop . " ... ";
echo $text;
return;
}
function compress($string, $first, $last) {
while(stripos($string,$first) && stripos($string,$last)) {
$string = substr_replace($string, "", stripos($string,$first), stripos($string,$last) - stripos($string,$first) + strlen($last));
}
return $string;
}
function directoryToArray($directory, $recursive) {
$array_items = array();
if ($handle = opendir($directory)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (is_dir($directory. "/" . $file)) {
if($recursive) {
$array_items = array_merge($array_items, directoryToArray($directory. "/" . $file, $recursive));
}
} else {
$file = $directory . "/" . $file;
$array_items[] = preg_replace("/\/\//si", "/", $file);
}
}
}
closedir($handle);
}
return $array_items;
}
function filewalk($file, $search, $counter, $webpath) {
if (strtolower(substr($file, stripos($file, ".htm"))) == ".htm"
|| strtolower(substr($file, stripos($file, ".html"))) == ".html"
|| strtolower(substr($file, stripos($file, ".asp"))) == ".asp"
|| strtolower(substr($file, stripos($file, ".php"))) == ".php") {
$all = file_get_contents($file);
$body = substr($all, stripos($all,"<body"),stripos($all,"</body>") - stripos($all,"<body"));
$body = preg_replace('/<br \/>/i', ' ', $body);
$body = preg_replace('/<br>/i', ' ', $body);
$body = compress($body,"<noscript","</noscript>");
$body = compress($body,"<script","</script>");
$body = compress($body,"<iframe","</iframe>");
$body = compress($body,"<noframe","</noframe>");
$body = strip_tags($body);
$body = html_entity_decode($body, ENT_QUOTES);
$body = preg_replace('/\s+/', ' ', $body);
if (stripos($body, $search)) {
$title = substr($all, stripos($all,"<title>") + 7,stripos($all,"</title>") - stripos($all,"<title>") - 7);
$title = html_entity_decode($title, ENT_QUOTES);
$title = preg_replace('/\s+/', ' ', $title);
echo '<p><a href="' . $file . '">' . $title . '</a></br>';
echo '<span id="webpath">' . $webpath . substr($file, stripos($file, "/")) . '</span><br />';
echo textpart($body, $search) . '</p>';
$counter = $counter + 1;
}
}
return $counter;
}
$url = $_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://';
$url .= $_SERVER['SERVER_PORT'] != '80' ? $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"] : $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (stripos($url,"?search=")) $search = $_GET['search'];
$webpath = dirname($url);
if (strlen($search) < 2 && trim($search) <> "") {
echo '<p>' . $to_small . '!</p>';
$search = "";
}
if (trim($search) <> "") {
echo "<p>" . $search_at . " '<b>" . $search . "</b>' " . $search_result . ".</p>";
$counter = 0;
$curdir = getcwd();
if ($dir = opendir($curdir)) {
$files = directoryToArray("./", $recursive);
foreach ($files as $file) {
$counter = filewalk($file, $search, $counter, $webpath);
}
closedir($dir);
}
echo "<p>" . $pages . ": " . $counter . "</p>";
}
?>
Kommentar