Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente di HTML.it
    Registrato dal
    Jul 2002
    Messaggi
    348

    Evidenziazione risultati di ricerca motore interno al sito

    Ciao a tutti...

    Prima di scrivere mi sono spaccato la testa e ho già cercato a destra e a manca una soluzione ma ho dei problemi!

    Cosa voglio fare è chiaro nell'oggetto.

    Dopo aver cercato le pagine dove sono contenute le parole di ricerca chiamo una pagina viewresult.asp che tramite XMLHTTP mi estrae dal file originale una stringa contenente tutto il codice HTML che poi vado a manipolare per evidenziare i risultati.

    Ho provato due soluzioni ma entrambe purtroppo hanno dei problemi e per questo chiedo aiuto...

    Soluzione A - codice trovato in giro nel forum:

    function hitLite (sSummary,sQueryString)
    Dim arrQueryString
    Dim iQs
    Dim i
    Dim arrSummary
    Dim sArrQueryStringTmp
    Dim iComp
    Dim sArrSummaryTmp
    Dim sRoot
    Dim sLastChar
    Dim sLength

    ' break the variable we want To compare querystring to into an array
    arrSummary = split(sSummary," ")



    ' break querystring into an array, use space For delimiter
    arrQueryString = split(sQuerystring," ", -1, 1)

    ' For every word In the querystring
    For iQs = 0 To ubound(arrQuerystring)

    ' assign the value To a temp variable
    sArrQueryStringTmp = arrQuerystring(iQs)

    ' don't include common search words, you can take this out. I was using this For a search engine
    'if (sArrQueryStringTmp <> "and") and (sArrQueryStringTmp <> "or") and (sArrQueryStringTmp <> "+") Then
    if (sArrQueryStringTmp <> "and") and (sArrQueryStringTmp <> "or") Then
    ' For Each element In the variable array, replace querystring word in varaible array With the word plus bgcolor ( highlight )
    For i = 0 To ubound(arrSummary)

    sArrSummaryTmp = arrSummary(i)

    ' if the 2 strings compare, stick the display value in a span With style!
    if strcomp(sArrSummaryTmp,sArrQueryStringTmp,1) = 0 Then
    arrSummary(i) = "<span class=""risultato"">" & sArrSummaryTmp & "</span>"
    Else
    ' check "s", comma, period
    ' must End "s" or comma or period AND be greater than 1 character
    sLastChar = right(sArrSummaryTmp,1)
    sFirstChar = left(sArrSummeryTmp,1)
    sLast2Char = right(sArrSummaryTmp,2)
    sLength = len(sArrSummaryTmp)

    if (sLastChar = "s") or (sLastChar = ".") or (sLastChar = ",") or (sLastChar = ";") or (sLastChar = """") or (sLastChar = "<") or (sFirstChar = """") or (sLast2Char = """;") or (sLast2Char = """.") or (sLast2Char = """,") and (sLength > 1) Then

    'the word minus last letter
    sRoot = left(sArrSummaryTmp,sLength-1)

    ' if the root comapres To the querystring
    ' replace that element of the array With the root highlited and the last character In regular display
    if strcomp(sRoot,sArrQueryStringTmp,1) = 0 Then
    ' don't include the comma or period in the lite
    arrSummary(i) = "<span class=""risultato"">" & sRoot & "</span>" & sLastChar & " "
    End if
    End if

    if (sFirstChar = """") or (sLast2Char = """;") or (sLast2Char = """.") or (sLast2Char = """,") and (sLength > 1) Then

    'the word minus last letter
    sRoot = left(sArrSummaryTmp,sLength-2)

    ' if the root comapres To the querystring
    ' replace that element of the array With the root highlited and the last character In regular display
    if strcomp(sRoot,sArrQueryStringTmp,1) = 0 Then
    ' don't include the comma or period in the lite
    arrSummary(i) = "<span class=""risultato"">" & sRoot & "</span>" & sLastChar & " "
    End if
    End if


    End if

    Next

    End if

    Next
    hitLite = join(arrSummary, " ")

    End function

    Ho modificato leggermente la funzione per evidenziare le parole ad esempio tra apici o che hanno caratteri particolari subito dopo! Presenta però il problema che non mi considera le parole seguite subito da tag html (es. pippo</span><span>)... splittando la stringa sugli spazi la parola di fatto non è pippo ma tutto fino alla chiusura del secondo span e quindi non matcha il risultato.

    Soluzione B - sempre trovata in giro:

    Function hitLite(strTesto, strParola)
    ' Parametri:
    ' strTesto stringa in cui cercare
    ' strParola stringa da cercare
    ' strPre stringa da inserire prima di strParola
    ' strPost stringa da inserire dopo strParola
    Dim intPos, intLen, intLenAll
    Dim strTemp

    strPre = "<span class=risultato>"
    strPost = "</span>"

    strParole = Split(strParola, " ")

    For i = 0 To ubound(strParole)

    'strParola = Replace(strParola, "*", "")
    strParola = strParole(i)

    intLen = Len(strParola)
    intLenAll = intLen + Len(strPre) + Len(strPost) + 1

    strTemp = strTesto

    If intLen > 0 And Len(strTemp) > 0 Then
    intPos = InStr(1, strTemp, strParola, 1)
    Do While intPos > 0
    strTemp = Left(strTemp, intPos - 1) & strPre & Mid(strTemp, intPos, intLen) & strPost & Mid(strTemp, intPos + intLen)
    intPos = InStr(intPos + intLenAll, strTemp, strParola, 1)
    Loop
    End If

    strTesto = strTemp
    Next

    hitLite = strTemp


    End Function

    Funzione certamente meglio dato che non viene splittata la stringa sugli spazi e quindi la ricerca è più puntuale... problemi: sicuramente molto lento con più termini e poi, purtroppo essendo la stringa composta anche ad HTML mi modifica anche gli elementi di codice che non vanno modificati (es. pippo.jpg , se cerco pippo mi cambia il nome dell'immagine).

    C'è qualcuno che avendo avuto gli stessi problemi ha trovato una soluzione accettabile che elimini i problemi sopra menzionati???

    Grazie mille
    Edo

  2. #2
    Fa un replace dei termini di ricerca con uno contenente i termini racchiusi tra una classe che ne evidenzi il testo.

  3. #3
    Utente di HTML.it L'avatar di Galex
    Registrato dal
    Aug 2002
    Messaggi
    667
    io faccio così:

    codice:
    <style>
    #zexy{background-color:#FFFFC4;padding:1px;color:red;}
    </style>
    
    ...
    
    'esempio 1, ricerca x una sola parola: Evidenzia("pippo è simpatico", "pippo")
    'esempio 2, ricerca x + parole :Evidenzia("pippo è simpatico", "pippo|simpatico")
    
    
      	Function Evidenzia(testo, ricerca)
      		if testo <> "" AND ricerca <> "" then
      			  strEvidenzia = Split(ricerca,"|")
      				For y=0 to UBound(strEvidenzia)
      				testo = Replace(testo, strEvidenzia(y), "<span id='zexy'>"&strEvidenzia(y)&"</span>", 1, -1, 1)
      				next
      			Evidenzia = testo
      		else
      			Evidenzia = testo
      		end if
      	End Function


Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.