Ciao a tutti...
Prima di scrivere mi sono spaccato la testae 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

e ho già cercato a destra e a manca una soluzione ma ho dei problemi!
Rispondi quotando
