...

View Full Version : Need help with this Search & replace script



tcadieux
01-21-2007, 11:17 PM
This script works quite well, except for one fairly significant issue. It find General ad highlights it, it finds Lieutenant and highlights it as well, but Lieutenant-General, (which is a keyword) is highlighted using the two seperate keywords, rather than the correct signle keyword. Code is below.



'<--------------------- Get the Article ------------------------->
'Get the Article and store it in an array
strSQL = "SELECT * FROM Articles_e WHERE '" & Date() & "' >= Publish_Date "
strSQL =strSQL & " AND ID = '"&iRecordId&"' "
strSQL =strSQL & " ORDER BY Article_date DEsc, RANK"

Set cnnGetRows = Server.CreateObject("ADODB.Connection")
cnnGetRows.Open Application("Connection1_ConnectionString")

Set rstGetRows = cnnGetRows.Execute(strSQL)
If not rstGetRows.Eof Then
arrArticle = rstGetRows.GetRows(1, adBookmarkCurrent, Array("id", "Article", "Photo1", "Photo1_ALT", "Photo1_Caption", "Pull_Quote1", _
"Photo2", "Photo2_ALT", "Photo2_Caption", "Pull_Quote2", "Photo3", "Photo3_ALT", "Photo3_Caption", "Pull_Quote3", "Headline" _
, "Main_Photo", "Main_Photo_Alt", "Main_Photo_Caption", "Article_Date"))


rstGetRows.Close
Set rstGetRows = Nothing
cnnGetRows.Close
Set cnnGetRows = Nothing
'<--------------------- END Get the Article ------------------------->

'<--------------------- Get the Glossary Definitions ------------------------->
'Get the Glossary Definitions and store them in an array
set conn=Server.CreateObject("ADODB.Connection")

strSQL=" SELECT UnitNameID AS GlossaryId , Cast(UName_E as varchar(8000))AS Term_e,"
strSQL=strSQL&" Cast(UAbbr_E as varchar(8000)) as Desc_e, 1 AS TableName, Cast(UAbbr_E as varchar(8000)) AS SearchTerm, Img As theImg FROM UnitNames "
strSQL=strSQL&" UNION SELECT ProductInfoID AS GlossaryId, Cast(ProductName as varchar(8000)) AS Term_e, "
strSQL=strSQL & " Cast(ProductDesc as varchar(8000))AS Desc_e, 2 AS TableName, Cast(ProductSearchTerm as varchar(8000)) AS SearchTerm, ProductSmallImage As theImg FROM Equip_ProductName"
strSQL=strSQL & " Union SELECT Rank_ID AS GlossaryId , Cast(Rank_e as varchar(8000)) AS Term_e, "
strSQL=strSQL & " Cast(Code_e as varchar(8000)) AS Desc_e, 3 AS TableName, Cast(Code_e as varchar(8000)) AS SearchTerm, Rank_e As theImg FROM Military_Rank "

trSQL=strSQL & " Order by Code_e"

Set cnnGetRows = Server.CreateObject("ADODB.Connection")
cnnGetRows.Open Application("Connection1_ConnectionString")

Set rstGetRows = cnnGetRows.Execute(strSQL)
'arrGlossary = rstGetRows.GetRows(, adBookmarkCurrent, Array("GlossaryId","Term_e","Desc_e","TableName" ))
arrDefinitions = rstGetRows.GetRows(, adBookmarkCurrent, Array("GlossaryId","Term_e","Desc_e","TableName","SearchTerm","theImg"))

rstGetRows.Close
Set rstGetRows = Nothing
cnnGetRows.Close
Set cnnGetRows = Nothing
'<--------------------- END Get the Glossary Definitions ------------------------->

'<------- Save the Article from the Array into a Variable which can be manipulated ---------->
IF isArray(arrArticle) THEN
Article=arrArticle(1,0)
END IF
'<------- END Save the Article from the Array into a Variable which can be manipulated ---------->

'<------- Loop through all the Definitions ----------------------->
' IF a Defintion is Found as text in the Article Variable, use the Highlight Function to Add A HREF code to it --->
For I = LBound(arrDefinitions, 2) To UBound(arrDefinitions, 2)
'CHECK THE ARTICLE VARIABLE FOR THE DEFINITION - ITEM NAME
If InStr(LCASE(Article),LCASE(arrDefinitions(1,I))) Then
IF arrDefinitions(3,I)="1" THEN '1 For Units <=========================================================
If NOT arrDefinitions(5,I)="" OR isNull(arrDefinitions(5,I)) THEN
theImg="http://www.army.forces.gc.ca/lf/images/7_0/"&arrDefinitions(5,I)
END IF
ELSEIF arrDefinitions(3,I)="2" THEN '2 For Equipment <=========================================================
If NOT arrDefinitions(5,I)="" OR isNull(arrDefinitions(5,I)) THEN
theImg="http://www.army.forces.gc.ca/lf/images/2_0/"&arrDefinitions(5,I)
END IF
ELSEIF arrDefinitions(3,I)="3" THEN '3 For Rank <=========================================================
If NOT arrDefinitions(4,I)="" OR isNull(arrDefinitions(4,I)) THEN
theImg="http://www.army.forces.gc.ca/lf/images/1_0/"&arrDefinitions(4,I)&".jpg"
END IF
END IF

Article = Highlight(Article, arrDefinitions(1,I), "<a href='4_7.asp?id="&arrDefinitions(0,I) &"&tbl="&arrDefinitions(3,I)& _
"' Class='Underline' Target='New' rel='balloon"&I&"'>", "</a> ")
Footer = Footer & "<div id='balloon"&I&"' class='balloonstyle'>" & _
"<img src='"&theImg&"' style='float: Center; ' />" & _
"<br>For more information on &quot;"&arrDefinitions(1,I)&"&quot;, follow the link." & _
"</div>" & vbCrLf
End If

'CHECK THE ARTICLE VARIABLE FOR THE DEFINITION - ITEM ABBREVIATION
IF NOT isNull(arrDefinitions(4,I)) AND NOT arrDefinitions(4,I)="" THEN
IF arrDefinitions(3,I)="1" THEN '1 For Units <=========================================================
If NOT arrDefinitions(5,I)="" OR isNull(arrDefinitions(5,I)) THEN
theImg="http://www.army.forces.gc.ca/lf/images/7_0/"&arrDefinitions(5,I)
END IF
ELSEIF arrDefinitions(3,I)="2" THEN '2 For Equipment <=========================================================
If NOT arrDefinitions(5,I)="" OR isNull(arrDefinitions(5,I)) THEN
theImg="http://www.army.forces.gc.ca/lf/images/2_0/"&arrDefinitions(5,I)
END IF
ELSEIF arrDefinitions(3,I)="3" THEN '3 For Rank <=========================================================
If NOT arrDefinitions(4,I)="" OR isNull(arrDefinitions(4,I)) THEN
theImg="http://www.army.forces.gc.ca/lf/images/1_0/"&arrDefinitions(4,I)&".jpg"
END IF
END IF
If InStr(LCASE(Article),LCASE(arrDefinitions(4,I))) Then
Article = Highlight(Article, arrDefinitions(4,I), "<a href='4_7.asp?id="&arrDefinitions(0,I) &"&tbl="&arrDefinitions(3,I)& _
"' Class='Underline' Target='New' rel='balloon"&I&"z'>", "</a> ")
Footer = Footer & "<div id='balloon"&I&"z' class='balloonstyle'>" & _
"<img src='"&theImg&"' style='float: Center; '/>" & _
"<br>For more information on &quot;"&arrDefinitions(4,I)&"&quot;, follow the link." & _
"</div>" & vbCrLf
End If
END IF

Next
'<------- END Loop through all the Definitions ----------------------->
<%= Replace(Article,"''","'")%>

degsy
01-23-2007, 02:49 PM
I can't see your function code, but I suspect it is using simple string functions for the replace.

In that case all I could suggest would be to check for space before & after a keyword.

e.g. " General "


Otherwise you may have to look for a more complex regular expression.

tcadieux
01-23-2007, 05:52 PM
quite right, it's working now! :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum