Autor | Zpráva | ||
---|---|---|---|
Mozkomor05 Profil |
Dobrý den, mohli byste mi prosím pomoc. Snažím se udělat, že slovo, které uživatel zadal, aby bylo ve výsledcích tučně. Všechny mé výsledky vždy končí chybou nebo ničím:)
Děkuji za odpověď Kód je zde: <?php $host = "IP"; $dbuser = "UŽIVATEL"; $dbpass = "HESLO"; $db = "DATABÁZE"; $con = mysql_connect($host, $dbuser, $dbpass); if(!$con){ die(mysql_error()); } $select = mysql_select_db($db, $con); if(!$select){ die(mysql_error()); } $item = (isset($_GET['find']) ? $_GET['find'] : ""); if (empty($item)) { return $message = "<b>Zadejte klíčové slovo a stiskněte tlačítko hledat.</b>"; } $data = mysql_query("SELECT * FROM hledat_web WHERE Nadpis LIKE ('%$item%')"); $cislo = mysql_num_rows($data); echo "<table><tr><td>Počet výsledků pro klíčové slovo "<b>$item</b>": <b style='font-size: 16px'>$cislo</b></td></tr>"; while($row = mysql_fetch_array($data)){ echo "<tr><td><br><br></td></tr>"; echo "<tr><td><a href=".$row['Odkaz']."><span style='font-size: 20px; padding: 0 50px;'>".$row['Nadpis']."</span></a><br><span style='color: rgb(0,102,33);'>".$row['Microdata']."</span><br><p style='text-align:justify;'>".$row['Text']."</p></td></tr>"; } echo "</table>"; if(mysql_num_rows($data)==0) echo "<b>Nic nebylo nalezeno.</b> Jste si jistí, že tato položka existuje? "; ?> |
||
Slark Profil * |
#2 · Zasláno: 5. 3. 2016, 20:29:20
Zkus řádek Počet výsledků pro klíčové slovo.. nahradit za:
echo "<table><tr><td>Počet výsledků pro klíčové slovo "<b>".$item."</b>": <b style='font-size: 16px'>".$cislo."</b></td></tr>"; Popř. napiš jakou konkrétně chybou tvé výsledky končí. Doplním, že požívání mysql_* funkcí je již zasaralé a chtělo by to nahradit alespoň za mysqli_* + dotaz mysql_query nahraď za : mysql_query("SELECT * FROM hledat_web WHERE Nadpis LIKE '%".mysql_real_escape_string($item)."%' "); |
||
juriad Profil |
#3 · Zasláno: 5. 3. 2016, 20:48:33
Asi chceš také nahradit:
.$row['Text']. . str_replace($item, '<b>' . $item . '</b>', $row['Text'])) . Pokud chceš dělat cokoli složitějšího (řešit velikost písmen, text s/bez diakritiky), budeš si na to asi muset napsat vlastní funkci. |
||
Mozkomor05 Profil |
#4 · Zasláno: 7. 3. 2016, 19:39:00
juriad:
Když jsem přidal před str_replace závorku funguje to. Díky přesně tohle jsem hledal.
|
||
juriad Profil |
#5 · Zasláno: 7. 3. 2016, 19:42:17
Mozkomor05:
Jo, uklepl jsem se a omylem napsal dvě závorky. Mohl jsi tu poslední jen odmazat. |
||
Časová prodleva: 12 dní
|
|||
Mozkomor05 Profil |
#6 · Zasláno: 19. 3. 2016, 11:42:36
juriad:
„Pokud chceš dělat cokoli složitějšího (řešit velikost písmen, text s/bez diakritiky), budeš si na to asi muset napsat vlastní funkci.“ Písmo bez diakritiky řešit nepotřebuju, ale velikost písmen mi dělá trochu problém, protože pokud je v textu například slovo AHOJ velkými písmeny a já vyhledám ahoj malým písmem, tak se mi nevyznačí tučně. Mohli byste mi prosím pomoc, děkuji za odpověď |
||
juriad Profil |
#7 · Zasláno: 19. 3. 2016, 12:11:43
Tak na to musíš použít regulární výrazy:
. preg_replace('/' . preg_quote($item, '/') . '/i', '<b>\0</b>', $row['Text']) . |
||
Mozkomor05 Profil |
#8 · Zasláno: 19. 3. 2016, 13:12:13
Děkuji funguje (i když přesně nevím jak)
|
||
juriad Profil |
Funguje to tak, že z toho, co hledáš - řekněme „ahoj“, udělá regulární výraz „/ahoj/i“. Lomítka jsou oddělovače, „i“ znamená bez ohledu na velikost písmen.
Kdykoli tedy najde něco, co vyhovuje regexu, uloží se to do skupiny číslo 0. Nalezený výskyt se nahradí skupinou 0 obalenou tagem pro tučnost. To zkráceně znamená, že to, co najde, obalí tagem b. Ta šaškárna s preg_quote je tam kvůli tomu, kdyby uživatel do vyhledávání zadal některý ze znaků: . \ + * ? [ ^ ] $ ( ) { } = ! < > | : - / . Ty totiž v regexu normálně mají speciální význam, což nechceš.
|
||
Kubo2 Profil |
#10 · Zasláno: 19. 3. 2016, 18:43:09
juriad:
„Tak na to musíš použít regulární výrazy:“ Prečo hneď tak striktne. Pokiaľ viem, na to isté sa dá efektívne využiť str_ireplace () , čo je varianta str_replace() neberúca ohľad na veľkosť písmen.
. str_ireplace($item, '<b>' . $item . '</b>', $row['Text']) . Niekde som videl zaujímavý český článok o situáciách, k ich riešeniu naozaj nie je potrebné použiť regulárne výrazy, len ho teraz bohužiaľ neviem spätne dohľadať. |
||
juriad Profil |
#11 · Zasláno: 19. 3. 2016, 19:02:27
Kubo2:
On ale nechce nahradit AHOJ za <b>ahoj<b> .
O moc efektivněji to řešit nelze. Nějaké harakiri s stripos by mohlo být rychlejší, ale toto rozhodně nebude dominovat času na vypsání stránky. Úzkým hrdlem bude ten databázový dotaz pomocí LIKE %...% . Nemá smysl se zabývat optimalizací.
|
||
Mozkomor05 Profil |
Děkuji všem za odpověď.
Ještě bych chtěl vyřešit jeden problém. Chtěl bych udělat to, že když někdo zadá slovo jsem, aby mu ve výsledcích nevyskočil celý článek, ale pouze část článku, kde se nejvíce nachází slovo jsem. Dejme tomu 150 znaků. Mohli byste mi prosím poradit jak to udělat? Předem děkuji za odpověď. Zdrojový kód s formulářem je zde: <?php require_once('hledej.php'); ?> <div class="hledat"> <form action="formular.php" method="get"> <fieldset> <input type="hidden" name="parent_id" value="0" /> <legend> <strong>Hledat na webu</strong> </legend> <?php if(!empty($message_uspech)){ echo '<ul><li class="green">'.$message_uspech.'</li></ul>'; } ?> <?php if(!empty($message)){ echo '<ul><li class="red">'.$message.'</li></ul>'; } ?> <label class="label">Hledat: <small>(Klíčová slova)</small></label> <input name="find" type="search" style="width: 25%; height: 30px;" value="<?php if(isset($_GET['find'])){ echo $_GET['find']; }?>" placeholder="Klíčové slovo" /> <button type="submit">HLEDAT</button> <br /> <br /> <label class="label">Možnosti hledání:</label> <input name="searchphrase" id="searchphraseall" checked="checked" value="all" type="radio"><label for="searchphraseall">Všechna slova</label><span style="color:#9eb23f;">|</span><input name="searchphrase" id="searchphraseexact" value="exact" type="radio"><label for="searchphraseexact">Přesná fráze</label><span style="color:#9eb23f;">|</span> <br /> <label class="label">Prohledávat:</label> <input name="vse" type="checkbox" checked="checked"><span>Prohledávat vše</span><span style="color:#9eb23f;">|</span><input name="clanky" type="checkbox"><span>Články</span><span style="color:#9eb23f;">|</span><input name="foto" type="checkbox"><span>Foto a video galerie</span><span style="color:#9eb23f;">|</span><input name="ostatni" type="checkbox"><span>Ostatní</span><span style="color:#9eb23f;">|</span> </fieldset> </form> </div> <br> <?php if (empty(!empty($message))){ while($row = mysql_fetch_array($data)){ echo "<table><tr><td><br><br></td></tr>"; echo "<tr><td><span class='video' style='font-size: 20px; padding: 0 50px;'><a href=".$row['Odkaz'].">". $row['Nadpis'] ."</a></span><br><span style='color: #00FF00;'>".$row['Microdata']."</span><br><p style='text-align:justify;'>". preg_replace('/' . preg_quote($item, '/') . '/i', '<span style="font-weight: 900;color: #9EB23F;">\0</span>', $row['Text']) ."</p></td></tr>"; echo "</table>";}} <?php // prihlaseni do databaze $host = "IP"; $dbuser = "UŽIVATEL"; $dbpass = "HESLO"; $db = "DATABÁZE"; // zpracovani databaze $con = mysql_connect($host, $dbuser, $dbpass); if(!$con){ die(mysql_error()); } $select = mysql_select_db($db, $con); if(!$select){ die(mysql_error()); } // item je políčko s názvem find $item = (isset($_GET['find']) ? $_GET['find'] : ""); // hlaska, ze navstevnik nezadal hledane slovo if (empty($item)) { return $message = "Zadejte klíčové slovo a stiskněte tlačítko hledat. "; } // minimalni pocet znaku jsou ctyri if (strlen($item) <=3){ return $message = "Minimální počet znaků v klíčovém slově je čtyři! "; } // vyhledani tabulky v databazi $data = mysql_query("SELECT * FROM `hledat_web` WHERE `Nadpis` LIKE '%$item%' OR `Text` LIKE '%$item%'"); // pocet vysledku pro klicove slovo $cislo = mysql_num_rows($data); if (empty(mysql_num_rows($data)==0)){ $message_uspech = "Počet výsledků pro klíčové slovo "<b>".$item."</b>": <b style='font-size: 16px'>".$cislo."</b>"; } // hlaska, ktera rika, ze nebylo nic nalezeno if(mysql_num_rows($data)==0) $message = "<b>Nic nebylo nalezeno.</b> Jste si jistí, že tato položka existuje? "; mysql_close ($con); ?> |
||
Mozkomor05 Profil |
když za
$row['Text'] vložím ,0,150,'UTF-8' tak mi to píše chybu
|
||
juriad Profil |
Můžeš použít tuto funkci excerpt, která dělá následující:
* najde úsek textu o maximální délce $length, který obsahuje maximum výskytů; neobsahuje-li text žádný vyskyt, vezme počátek textu * takto vybraný region rozšíří na celá celá slova (oddělovačem je nový řádek nebo mezera) * následně rozšiřuje region po jednom slově na začátku a na konci regionu, dokud velikost nepřekročí $length * nakonec zvýrazní výskyty pomocí zadané funkce * všechny funkce předpokládají, že mb_internal_encoding() vrací správnou hodnotu <?php function excerpt($text, $search, $length = 150, $wrap = NULL) { $searchLength = mb_strlen($search); $textLength = mb_strlen($text); # let's save positions of all occurences into an array $positions = []; $pos = 0; while (($pos = mb_stripos($text, $search, $pos)) !== FALSE) { $positions[] = $pos; $pos += $searchLength; } var_dump($positions); if (count($positions) == 0) { # couldn't find $search anywhere # let's return part of the text from the beginning $begin = 0; $end = min($length, $textLength); } else { # let's find longest subsequence (of limited length) which contains most occurences $bestFrom = -1; $bestTo = -1; $from = 0; # $from is tail, $to is head for ($to = 0; $to < count($positions); $to++) { # moving the tail to keep the body <= $length while ($positions[$to] - $positions[$from] + $searchLength > $length) { $from++; } # does the body contain more occurences? if ($to - $from + 1 > $bestTo - $bestFrom) { $bestFrom = $from; $bestTo = $to; } } $begin = $positions[$bestFrom]; $end = $positions[$bestTo] + $searchLength; } var_dump(array('begin' => $begin, 'end' => $end)); # now we have estimates in $begin and $end, let's extend it as much as possible to size $length # we will alternate extending the region left and right until we excess the $length # we extends at least once to make sure we are at the border of a word # we assume that the words are separate by spaces or newlines $delta = 0; do { # find next space/nl $endOffset = min($end + $delta, $textLength); $endPos1 = mb_stripos($text, ' ', $endOffset); $endPos2 = mb_stripos($text, "\n", $endOffset); $end = min($endPos1 === FALSE ? $textLength : $endPos1, $endPos2 === FALSE ? $textLength : $endPos2); # find previous space/nl $beginOffset = max($begin - $textLength - 1 - $delta, -$textLength); $beginPos1 = mb_strripos($text, ' ', $beginOffset); $beginPos2 = mb_strripos($text, "\n", $beginOffset); $begin = max($beginPos1 === FALSE ? -1 : $beginPos1, $beginPos2 === FALSE ? -1 : $beginPos2) + 1; # this makes sure we look for border of the word for the first time, not further if ($delta == 0) { $delta = 1; } var_dump(array('begin' => $begin, 'end' => $end)); } while($end - $begin + 1 < $length && ($begin != 0 || $end != $textLength)); # now the $begin and $end form a region of words, time to wrap the search results $excerpt = mb_substr($text, $begin, $end - $begin + 1); if (is_callable($wrap)) { $regex = '/' . preg_quote($search, '/') . '/i'; $excerpt = preg_replace_callback($regex, $wrap, $excerpt); } return $excerpt; } # function which is highlighting the found item function boldize($matches) { return '<b>' . $matches[0] . '</b>'; } # example usage echo excerpt($row['Text'], $item, 150, 'boldize'); |
||
Mozkomor05 Profil |
Mám to dát do souboru hledej.php nebo do souboru s formulářem? Píše mi to totiž chybu
Aha, už to funguje, ale místo zkráceného článku mi to píše: array(0) { } array(2) { ["begin"]=> int(0) ["end"]=> int(0) } array(2) { ["begin"]=> int(0) ["end"]=> int(0) } |
||
juriad Profil |
#16 · Zasláno: 21. 3. 2016, 16:39:41
Mozkomor05:
Patří to do souboru hledej.php Protože jsem nechal na řádcích 14, 43, 68 var_dump. Toto by to ale nemělo vypisovat; jsi si jistý, že předáváš funkci excerpt správné parametry?
|
||
Mozkomor05 Profil |
Když jsem dal do hledej. php toto: (odstranil jsem tam var_dump)
<?php function excerpt($text, $search, $length = 150, $wrap = NULL) { $searchLength = mb_strlen($search); $textLength = mb_strlen($text); # let's save positions of all occurences into an array $positions = []; $pos = 0; while (($pos = mb_stripos($text, $search, $pos)) !== FALSE) { $positions[] = $pos; $pos += $searchLength; } ($positions); if (count($positions) == 0) { # couldn't find $search anywhere # let's return part of the text from the beginning $begin = 0; $end = min($length, $textLength); } else { # let's find longest subsequence (of limited length) which contains most occurences $bestFrom = -1; $bestTo = -1; $from = 0; # $from is tail, $to is head for ($to = 0; $to < count($positions); $to++) { # moving the tail to keep the body <= $length while ($positions[$to] - $positions[$from] + $searchLength > $length) { $from++; } # does the body contain more occurences? if ($to - $from + 1 > $bestTo - $bestFrom) { $bestFrom = $from; $bestTo = $to; } } $begin = $positions[$bestFrom]; $end = $positions[$bestTo] + $searchLength; } (array('begin' => $begin, 'end' => $end)); # now we have estimates in $begin and $end, let's extend it as much as possible to size $length # we will alternate extending the region left and right until we excess the $length # we extends at least once to make sure we are at the border of a word # we assume that the words are separate by spaces or newlines $delta = 0; do { # find next space/nl $endOffset = min($end + $delta, $textLength); $endPos1 = mb_stripos($text, ' ', $endOffset); $endPos2 = mb_stripos($text, "\n", $endOffset); $end = min($endPos1 === FALSE ? $textLength : $endPos1, $endPos2 === FALSE ? $textLength : $endPos2); # find previous space/nl $beginOffset = max($begin - $textLength - 1 - $delta, -$textLength); $beginPos1 = mb_strripos($text, ' ', $beginOffset); $beginPos2 = mb_strripos($text, "\n", $beginOffset); $begin = max($beginPos1 === FALSE ? -1 : $beginPos1, $beginPos2 === FALSE ? -1 : $beginPos2) + 1; # this makes sure we look for border of the word for the first time, not further if ($delta == 0) { $delta = 1; } (array('begin' => $begin, 'end' => $end)); } while($end - $begin + 1 < $length && ($begin != 0 || $end != $textLength)); # now the $begin and $end form a region of words, time to wrap the search results $excerpt = mb_substr($text, $begin, $end - $begin + 1); if (is_callable($wrap)) { $regex = '/' . preg_quote($search, '/') . '/i'; $excerpt = preg_replace_callback($regex, $wrap, $excerpt); } return $excerpt; } # function which is highlighting the found item function boldize($matches) { return '<b>' . $matches[0] . '</b>'; } # example usage echo excerpt($row['Text'], $item, 150, 'boldize') ?> Ano jsem si jistý s tím excerpt mám tam správné údaje do MySQL |
||
juriad Profil |
#18 · Zasláno: 21. 3. 2016, 16:53:27
Ukaž řádek, kde ten
excerpt používáš. V kódu uvedeném v [#1] by tomu odpovídal řádek 26. řádek.
|
||
Mozkomor05 Profil |
Když to vložím do řádku 26 tak mi to nefunguje. Ahá, já jsem to špatně řekl ten kód jsem dal do souboru s formulářem. Když ho dám do hledej.php tak to vůbec nefunguje.
<?php require_once('hledej.php'); ?> <p></p> <div class="hledat"> <form action="zkouska.php" method="get"> <fieldset> <input type="hidden" name="parent_id" value="0" /> <legend> <strong>Hledat na webu</strong> </legend> <?php if(!empty($message_uspech)){ echo '<ul><li class="green">'.$message_uspech.'</li></ul>'; } ?> <?php if(!empty($message)){ echo '<ul><li class="red">'.$message.'</li></ul>'; } ?> <label class="label">Hledat: <small>(Klíčová slova)</small></label> <input name="find" type="search" style="width: 25%; height: 30px;" value="<?php if(isset($_GET['find'])){ echo $_GET['find']; }?>" placeholder="Klíčové slovo" /> <button type="submit">HLEDAT</button> <br /> <br /> <label class="label">Možnosti hledání:</label> <input name="searchphrase" id="searchphraseall" checked="checked" value="all" type="radio"><label for="searchphraseall">Všechna slova</label><span style="color:#9eb23f;">|</span><input name="searchphrase" id="searchphraseexact" value="exact" type="radio"><label for="searchphraseexact">Přesná fráze</label><span style="color:#9eb23f;">|</span> <br /> <label class="label">Prohledávat:</label> <input name="vse" type="checkbox" checked="checked"><span>Prohledávat vše</span><span style="color:#9eb23f;">|</span><input name="clanky" type="checkbox"><span>Články</span><span style="color:#9eb23f;">|</span><input name="foto" type="checkbox"><span>Foto a video galerie</span><span style="color:#9eb23f;">|</span><input name="ostatni" type="checkbox"><span>Ostatní</span><span style="color:#9eb23f;">|</span> </fieldset> </form> </div> <br> <?php if (empty(!empty($message))){ while($row = mysql_fetch_array($data)){ echo "<table><tr><td><br><br></td></tr>"; echo "<tr><td><span class='video' style='font-size: 20px; padding: 0 50px;'><a href=".$row['Odkaz'].">". $row['Nadpis'] ."</a></span><br><span style='color: #00FF00;'>".$row['Microdata']."</span><br><p style='text-align:justify;'>". preg_replace('/' . preg_quote($item, '/') . '/i', '<span style="font-weight: 900;color: #9EB23F;">\0</span>', $row['Text']) ."</p></td></tr>"; echo "</table>"; }} ?> <?php function excerpt($text, $search, $length = 150, $wrap = NULL) { $searchLength = mb_strlen($search); $textLength = mb_strlen($text); # let's save positions of all occurences into an array $positions = []; $pos = 0; while (($pos = mb_stripos($text, $search, $pos)) !== FALSE) { $positions[] = $pos; $pos += $searchLength; } ($positions); if (count($positions) == 0) { # couldn't find $search anywhere # let's return part of the text from the beginning $begin = 0; $end = min($length, $textLength); } else { # let's find longest subsequence (of limited length) which contains most occurences $bestFrom = -1; $bestTo = -1; $from = 0; # $from is tail, $to is head for ($to = 0; $to < count($positions); $to++) { # moving the tail to keep the body <= $length while ($positions[$to] - $positions[$from] + $searchLength > $length) { $from++; } # does the body contain more occurences? if ($to - $from + 1 > $bestTo - $bestFrom) { $bestFrom = $from; $bestTo = $to; } } $begin = $positions[$bestFrom]; $end = $positions[$bestTo] + $searchLength; } (array('begin' => $begin, 'end' => $end)); # now we have estimates in $begin and $end, let's extend it as much as possible to size $length # we will alternate extending the region left and right until we excess the $length # we extends at least once to make sure we are at the border of a word # we assume that the words are separate by spaces or newlines $delta = 0; do { # find next space/nl $endOffset = min($end + $delta, $textLength); $endPos1 = mb_stripos($text, ' ', $endOffset); $endPos2 = mb_stripos($text, "\n", $endOffset); $end = min($endPos1 === FALSE ? $textLength : $endPos1, $endPos2 === FALSE ? $textLength : $endPos2); # find previous space/nl $beginOffset = max($begin - $textLength - 1 - $delta, -$textLength); $beginPos1 = mb_strripos($text, ' ', $beginOffset); $beginPos2 = mb_strripos($text, "\n", $beginOffset); $begin = max($beginPos1 === FALSE ? -1 : $beginPos1, $beginPos2 === FALSE ? -1 : $beginPos2) + 1; # this makes sure we look for border of the word for the first time, not further if ($delta == 0) { $delta = 1; } (array('begin' => $begin, 'end' => $end)); } while($end - $begin + 1 < $length && ($begin != 0 || $end != $textLength)); # now the $begin and $end form a region of words, time to wrap the search results $excerpt = mb_substr($text, $begin, $end - $begin + 1); if (is_callable($wrap)) { $regex = '/' . preg_quote($search, '/') . '/i'; $excerpt = preg_replace_callback($regex, $wrap, $excerpt); } return $excerpt; } # function which is highlighting the found item function boldize($matches) { return '<b>' . $matches[0] . '</b>'; } # example usage echo excerpt($row['Text'], $item, 150, 'boldize') ?> |
||
juriad Profil |
#20 · Zasláno: 21. 3. 2016, 17:42:55
Mozkomor05:
Tu funkci excerpt dej někam na začátek toho souboru hledej.php (do toho, kde se vypisuje tabulka s výsledky).
Hned pod tu funkci excerpt dej funkci boldize . V ní budeš muset provést změnu, protože nechceš používat tag <b> :
return '<span style="font-weight: 900;color: #9EB23F;">' . $matches[0] . '</span>'; example usage zahodíš, to je jen nápověda, jak tu funkci excerp použít. Tys ji však nepochopil.
Výpis řádku tabulky, kde jsme ti dříve radili udělat změny (s tím str_replace, následně preg_replace), teď v tom posledním kódu je to 28. řádka si přeformátuješ, aby se v tom vůbec někdo vyznal. Následně nahradíš původní preg_replace za volání funkce excerpt :
echo "<tr> <td> <span class='video' style='font-size: 20px; padding: 0 50px;'> <a href=" . $row['Odkaz'] . ">" . $row['Nadpis'] . "</a> </span> <br> <span style='color: #00FF00;'>" . $row['Microdata'] . "</span> <br> <p style='text-align:justify;'>" . excerpt($row['Text'], $item, 150, 'boldize') . "</p> </td> </tr>"; Mimochodem, je dost špatný nápad všude cpát inline styly, proč těm elementům nepřiřadíš třídu, kterou potom v CSS nastyluješ? |
||
Mozkomor05 Profil |
#21 · Zasláno: 21. 3. 2016, 17:48:03
Díky, funguje
|
||
Mozkomor05 Profil |
Chtěl bych udělat ještě jedno vylepšení a to stránkování po deseti. Soubor se stránkováním už mám někde jinde, ale do fulltextového vyhledávání mi nějak nejde zabudovat.
<?php require_once('hledej.php');?> <div class="hledat"> <form action="zkouska.php" method="POST"> <fieldset> <input type="hidden" name="parent_id" value="0" /> <legend> <strong>Hledat na webu</strong> </legend> <?php if(!empty($message_uspech)){ echo '<ul><li class="green">'.$message_uspech.'</li></ul>'; } ?> <?php if(!empty($message)){ echo '<ul><li class="red">'.$message.'</li></ul>'; } ?> <label class="label">Hledat: <small>(Klíčová slova)</small></label> <input name="find" type="search" style="width: 25%; height: 30px;" value="<?php if(isset($_POST['find'])){ echo $_POST['find']; }?>" placeholder="Klíčové slovo" /> <button type="submit">HLEDAT</button> <br /> <br /> <label class="label">Možnosti hledání:</label> <label><input value="all" type="radio"/>Všechna slova</label><span style="color:#9eb23f;">|</span><label><input value="exem" type="radio"/>Přesná fráze</label><span style="color:#9eb23f;">|</span> </fieldset> </form> </div> <br> <?php include('includes/class.Paging.php'); $link = mysql_connect('88.86.117.154:3306', 'kolemzeme.wz3044', 'heslo'); if (!$link) { die('Could not connect: ' . mysql_error()); } if (!mysql_select_db('kolemzeme.wz3044')) { die('Could not select database: ' . mysql_error()); } $total = mysql_result(mysql_query("SELECT COUNT(*) FROM `hledat_web`"), 0); $limit = 20; $base = 'http://www.kolemzeme.wz.cz/zkouska.php/'; // celkem | [zakladna] | [po kolika] | [format parametru] $paging = new Paging($total, $base, $limit, '?page=%s'); $paging->set_title_format('Stránka %s'); // nepovinne $paging->set_around(2); // nepovinne $paging->set_paging_mode(1); // 0 $paging->set_output_mode(1); // 0 $paging->set_paging(); // vystup echo $paging->export_paging(); // echo $paging; // $tpl['paging'] = (string)$paging; // parametry limit pro sql dotaz: $sql_start = $paging->get_start(); $sql_limit = $paging->get_limit(); ?> <?php $print_paging = $paging->export_paging(); if(!empty($print_paging)){ ?> <p class="strankovani"> <strong>Stránkování:</strong><br /> <?php echo $print_paging; ?> </p> <?php }?> <?php if (empty(!empty($message))){ while($row = mysql_fetch_array($data)){ echo "<table><tr><td><br><br></td></tr>"; echo "<tr> <td> <span class='video' style='font-size: 20px; padding: 0 50px;'> <a href=" . $row['Odkaz'] . ">" . $row['Nadpis'] . "</a> </span> <br> <span style='color: #00FF00;'>" . $row['Microdata'] . "</span> <br> <p style='text-align:justify;'>..." . excerpt($row['Text'], $item, 450, 'boldize') . "...</p> </td> </tr>"; echo "</table>"; }} ?> <?php function excerpt($text, $search, $length = 450, $wrap = NULL) { $searchLength = mb_strlen($search); $textLength = mb_strlen($text); # let's save positions of all occurences into an array $positions = []; $pos = 0; while (($pos = mb_stripos($text, $search, $pos)) !== FALSE) { $positions[] = $pos; $pos += $searchLength; } ($positions); if (count($positions) == 0) { # couldn't find $search anywhere # let's return part of the text from the beginning $begin = 0; $end = min($length, $textLength); } else { # let's find longest subsequence (of limited length) which contains most occurences $bestFrom = -1; $bestTo = -1; $from = 0; # $from is tail, $to is head for ($to = 0; $to < count($positions); $to++) { # moving the tail to keep the body <= $length while ($positions[$to] - $positions[$from] + $searchLength > $length) { $from++; } # does the body contain more occurences? if ($to - $from + 1 > $bestTo - $bestFrom) { $bestFrom = $from; $bestTo = $to; } } $begin = $positions[$bestFrom]; $end = $positions[$bestTo] + $searchLength; } (array('begin' => $begin, 'end' => $end)); # now we have estimates in $begin and $end, let's extend it as much as possible to size $length # we will alternate extending the region left and right until we excess the $length # we extends at least once to make sure we are at the border of a word # we assume that the words are separate by spaces or newlines $delta = 0; do { # find next space/nl $endOffset = min($end + $delta, $textLength); $endPos1 = mb_stripos($text, ' ', $endOffset); $endPos2 = mb_stripos($text, "\n", $endOffset); $end = min($endPos1 === FALSE ? $textLength : $endPos1, $endPos2 === FALSE ? $textLength : $endPos2); # find previous space/nl $beginOffset = max($begin - $textLength - 1 - $delta, -$textLength); $beginPos1 = mb_strripos($text, ' ', $beginOffset); $beginPos2 = mb_strripos($text, "\n", $beginOffset); $begin = max($beginPos1 === FALSE ? -1 : $beginPos1, $beginPos2 === FALSE ? -1 : $beginPos2) + 1; # this makes sure we look for border of the word for the first time, not further if ($delta == 0) { $delta = 1; } (array('begin' => $begin, 'end' => $end)); } while($end - $begin + 1 < $length && ($begin != 0 || $end != $textLength)); # now the $begin and $end form a region of words, time to wrap the search results $excerpt = mb_substr($text, $begin, $end - $begin + 1); if (is_callable($wrap)) { $regex = '/' . preg_quote($search, '/') . '/i'; $excerpt = preg_replace_callback($regex, $wrap, $excerpt); } return $excerpt; } # function which is highlighting the found item function boldize($matches) { return '<span style="font-weight: 900;color: #9EB23F;">' . $matches[0] . '</span>'; } ?> $data = mysql_query("SELECT * FROM `hledat_web` WHERE `Nadpis` LIKE '%$item%' OR `Text` LIKE '%$item%'"); $data = mysql_query("SELECT * FROM `hledat_web` WHERE `Nadpis` LIKE '%$item%' OR `Text` LIKE '%$item%' DESC limit ".$paging->get_start().", ".$paging->get_limit()."")->assocList(); a: Fatal error: Call to a member function get_start() on null in /3w/wz.cz/k/kolemzeme/hledej.php on line 34 Předem děkuji za odpověď. |
||
Časová prodleva: 6 dní
|
|||
Mozkomor05 Profil |
#23 · Zasláno: 29. 3. 2016, 16:03:31
Tak nakonec jsem problém se stránkováním vyřešil.
Mám ještě jednu otázku. Dříve jsem psal, že diakritiku řešit nepotřebuji, ale to jsem přesně nepochopil co jsi tím myslel. Teď jsem zjistil, že pokud uživatel napsal slovo ZAJMY bez diakritiky a v článku je slovo ZÁJMY s diakritikou, tak to slovo nebude tučně, což bych nechtěl.Mohli byste mi prosím poradit jak mám udělat, aby to slovo bylo tučně? Děkuji za odpověď. |
||
juriad Profil |
#24 · Zasláno: 30. 3. 2016, 13:06:01
Ok, řešení diakritiky je o dost těžší problém. V podstatě jde o to, že musíš text i search očesat od diakritiky, pak provést vlastní výpočet, a až na konec zvýraznit čáti původního řetězce podle pozic v novém.
1) očesání diakritiky Zdá se to jako jednoduché, ale není. Použití hotových funckí jako je iconv nefunguje, protože každý znak na vstupu se převede na nějaký počet znaků výstupu. Při této transformaci se ztratí informace o posunu v řetězci. Například znak € se transformuje na EUR a německé ß na ss. Konverze by se musela provádět voláním iconv po znaku, což je dost výkonostně náročné. Nejlepší variantou se jeví být strtr, kterému předáš pole: znak s diakritikou => znak bez diakritiky. Pro jednoduché použití musíš zajistit, že se jeden znak přepíše na jeden znak. Tedy ß bude muset být prostě jen „s“. 2) rozšíření excerptu Skákat na po koncích slov je mnohem obtížnější než to na první pohled vypadá, protože se pletou dohromady bytové a znakové offsety. Pokud má být kód dost výkonný, neměl by se celý text procházet mnohokrát. Aby bylo řešení robustní, mělo by fungovat i v případě delších posloupností mezer, to jsem původně vůbec neřešil. 3) zvýraznění v původním řetězci Pozice známe. Stačí text rozsekat na úseky a pak jej opět spojit dohromady. Jelikož excerpt je krátký, můžeme tady strávit vic času a použit pohodlně funkce mb_substr, úseky vložit do pole a pak stmelit implodem. Zkouším to naprogramovat, ale udělat to dobře je hodně náročné. |
||
juriad Profil |
#25 · Zasláno: 30. 3. 2016, 16:57:16
Původní funkci
excerpt zahodíš a nahradíš ji třídou Excerpt : gist.github.com/juriad/60dc25807c59235417d72e6ffa96eefd
Budeš muset trochu poupravit funkci boldize - stačí odstranit indexování [0] .
Výpis v té své tabulce (na řádku 72) nahradíš za: . (new Excerpt($row['Text'], $item))->extract(450, 'boldize') . Toto už je dostatečně univerzální řešení, snad i rozšiřitelné pro další úpravy. |
||
Mozkomor05 Profil |
Nefunguje mi to. Píše to chybu vždy na konci souboru. Takže:
Parse error: syntax error, unexpected end of file in /3w/wz.cz/k/kolemzeme/zkouska.php on line 492. Když odstraním jeden řádek tak on line 491 Použil jsem ten kód takový, jaký jsem našel v odkazu. Jenom jsem $length = 150 přepsal na $length = 450
|
||
juriad Profil |
#27 · Zasláno: 30. 3. 2016, 17:48:47
Nahraj někam celý soubor, který jsi upravil. Někde jsi v něm něco špatně upravil. Svůj kód jsem důkladně otestoval. Jakou máš na serveru verzi PHP?
|
||
Mozkomor05 Profil |
Verze: PHP 5.6
Jinak jsem soubor nijak neupravil pouze to číslo. |
||
juriad Profil |
#29 · Zasláno: 30. 3. 2016, 18:10:50
To číslo jsi vůbec nemusel upravovat, jde o výchozí hodnotu parametru, pokud bys ji neuvedl. Ty ji ale při výpisu tabulky uvádíš explicitně.
Nejspíš jsi to nakopíroval na špatné místo nebo jsi část původního kódu nesmazal nebo tak něco. Bude to nějaká ptákovina, ale těžko říct jaká přesně. |
||
Mozkomor05 Profil |
Kopíruji to tam znovu a znovu. Do všech možných souborů, ale pořád to nefunguje.
Většinou mi to píše konkrétní chybu ale teď pořád poslední řádek. Celý kód souboru s formulářem se mi zde nevleze, proto ho nalezneš zde: www.kolemzeme.wz.cz/kod.txt |
||
Téma pokračuje na další straně.
|
0