Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

text highlighter 8

Status
Not open for further replies.

chinedu

Technical User
Mar 7, 2002
241
US
hello everyone,
I need a word highlighting script.
Does anyone know where I can find a sample.

The way I need to use this script is this:

I enter a word, say "ABC", if "ABC" exists on our database, all records associated with "ABC" will be displayed and the word "ABC" will be highlighted.

Any help would be greatly appreciated.
Thanks in advance
 
Hiya im trying to use this code but it isnt doing any thing

Code:
const HLStyle = "<span style='color:red;font-weight:bold;background-color:#FFFFCC'>"
const HLStyle2 = "<span style='color:blue;font-weight:bold;background-color:silver'>"

WordArr = Split(keywords," ")
for i = 0 to Ubound(WordArr)
bHL = False
bHL2 = False
    if WordArr(i) = s_search then bHL = True ' highlight this word 
    if PunctMarks(right(WordArr(i),1),"[.,]") then    'check for full stop or comma
        if s_search = left(WordArr(i),len(WordArr(i))-1) then bHL = True
        ' if word matches ignoring the last char highlight it
    end if
    if bAll then 
    ' highlight all instances
    if InStr(WordArr(i),s_search) <> 0 then bHL2 = True
    end if
    
    if bHL then
        TextOut = TextOut & " " & HLStyle & WordArr(i) & "</span>"
    elseif bHL2 then
        TextOut = TextOut & " " & HLStyle2 & WordArr(i) & "</span>"
    else
        TextOut = TextOut & " " &  WordArr(i)
    end if
next
HighLight = TextOut

 response.write HighLight

Keywords are e.g Uploading Help file
s_search are e.g Up

So wouldnt it highlight the Up in uploading ????

Thanks Chris
 
Sorry mate im new to asp and dont really understand i have this error

Error Type:
Microsoft VBScript compilation (0x800A0414)
Cannot use parentheses when calling a Sub
/ChrisDev/postit.asp, line 59, column 32
HighLight(s_search,Keyword,bAll)
-------------------------------^

and this is my code
Code:
const HLStyle = "<u><span style='color:red;font-weight:bold;background-color:#FFFFCC'></u>"
const HLStyle2 = "<i><b><span style='color:blue;font-weight:bold;background-color:silver'></b></i>"

WordArr = Split(Keyword," ")
for i = 0 to Ubound(WordArr)

' setting bAll to true here '''''
bAll = true 
'''''''''''''''''''''''''''''''''
bHL = False
bHL2 = False
    if WordArr(i) = s_search then bHL = True ' highlight this word 
    if PunctMarks(right(WordArr(i),1),"[.,]") then    'check for full stop or comma
        if s_search = left(WordArr(i),len(WordArr(i))-1) then bHL = True
        ' if word matches ignoring the last char highlight it
    end if
    if bAll then 
    ' highlight all instances
    if InStr(WordArr(i),s_search) <> 0 then bHL2 = True
    end if
    
    if bHL then
        TextOut = TextOut & " " & HLStyle & WordArr(i) & "</span>"
    elseif bHL2 then
        TextOut = TextOut & " " & HLStyle2 & WordArr(i) & "</span>"
    else
        TextOut = TextOut & " " &  WordArr(i)
    end if
next
HighLight = TextOut

''' Error here '''''
HighLight(s_search,Keyword,bAll)
''''''''''''''''''''

end function
 
you have the call to the function inside the function in your example.

it needs setting out as;

copy and paste the function code to a new file between ASP delimiters (<%), then save this to a file called inc_funcs_text.asp in your include folder.
Then at the top of any page where you need to use the function add a line (under <%option explicit%> of course)
Code:
<!--#include virtual="/lncludefoldername/inc_funcs_text.asp"-->

Call the function wherever you need it. examples of useage are shown above.


Chris.

Indifference will be the downfall of mankind, but who cares?
A website that proves the cobblers kids adage.
Nightclub counting systems

So long, and thanks for all the fish.
 
i get the error of

Code:
Error Type:
Microsoft VBScript runtime (0x800A01F4)
Variable is undefined: 'PunctMarks'
/chrisdev/incfuncstext.asp, line 29
 
sorry my bad this is the error

Code:
Error Type:
Active Server Pages, ASP 0141 (0x80004005)
The @ command can only be used once within the Active Server Page.
/chrisdev/incfuncstext.asp, line 1

incfuncstext.asp code is

Code:
<% @LANGUAGE = VBScript %>
<%Option Explicit%>

<%i_id = cint(request.querystring("ID"))
s_search = cstr(request.querystring("search"))
Set objConn = Server.CreateObject("ADODB.Connection")
strConnection = "DSN=CAGeHelp;Database=Front;"
strConnection = strConnection  & "UID=sa;PWD=;"
objConn.Open strConnection
strQuery = "SELECT Keywords, Helpinfo, Group, Subgroup FROM helpfile "
strQuery = strQuery & " WHERE HN = " & i_id 
strQuery = strQuery & " ORDER BY Keywords"
Set objRS = objConn.Execute(strQuery)
response.write (s_search  & "   -   ")

Keyword = objRS("Keywords")
response.write (keyword)

function HighLight(s_search,Keyword,bAll)
dim i,WordArr,TextOut,bHL,bHL2,PunctMarks

const HLStyle = "<u><span style='color:red;font-weight:bold;background-color:#FFFFCC'></u>"
const HLStyle2 = "<i><b><span style='color:blue;font-weight:bold;background-color:silver'></b></i>"

WordArr = Split(Keyword," ")
for i = 0 to Ubound(WordArr)

bHL = False
bHL2 = False
    if WordArr(i) = s_search then bHL = True ' highlight this word 
    if PunctMarks(right(WordArr(i),1),"[.,]") then    'check for full stop or comma
        if s_search = left(WordArr(i),len(WordArr(i))-1) then bHL = True
        ' if word matches ignoring the last char highlight it
    end if
    if bAll = true then 
    ' highlight all instances
    if InStr(WordArr(i),s_search) <> 0 then bHL2 = True
    end if
    
    if bHL = true then
        TextOut = TextOut & " " & HLStyle & WordArr(i) & "</span>"
    elseif bHL2 = true then
        TextOut = TextOut & " " & HLStyle2 & WordArr(i) & "</span>"
    else
        TextOut = TextOut & " " &  WordArr(i)
    end if
next
response.write(Textout)
HighLight = TextOut
bAll = true
response.write(bAll)
'response.write(<b>" " & Highlight & " "</b>)
End function %>
 
Hi thanks for the help but i already have this like in my code does this not call the function ?

<!--#include virtual="/chrisdev/incfuncstext.asp"-->
 
only if you've put the code for the function into it.

getting a "variable undefined" error means it didn't find the function name.



Chris.

Indifference will be the downfall of mankind, but who cares?
A website that proves the cobblers kids adage.
Nightclub counting systems

So long, and thanks for all the fish.
 
Im really dishearted that it does not work :(
here is my incfuncstext.asp
Code:
<%i_id = cint(request.querystring("ID"))
s_search = cstr(request.querystring("search"))
Set objConn = Server.CreateObject("ADODB.Connection")
strConnection = "DSN=CAGeHelp;Database=Front;"
strConnection = strConnection  & "UID=sa;PWD=;"
objConn.Open strConnection


function HighLight(TextIn,TextToHighlight,bAll)
dim i,WordArr,TextOut,bHL,bHL2

TextIn = s_search
TextToHighlight = keyword  

const HLStyle = "<span style='color:red;font-weight:bold;background-color:#FFFFCC'>"
const HLStyle2 = "<span style='color:blue;font-weight:bold;background-color:silver'>"

WordArr = Split(TextIn," ")
for i = 0 to Ubound(WordArr)
bHL = False
bHL2 = False
    if WordArr(i) = TextToHighlight then bHL = True ' highlight this word 
    if PM(right(WordArr(i),1),"[.,]") then    'check for full stop or comma
        if TextToHighlight = left(WordArr(i),len(WordArr(i))-1) then bHL = True
        ' if word matches ignoring the last char highlight it
    end if
    if bAll then 
    ' highlight all instances
    if InStr(WordArr(i),TextToHighlight) <> 0 then bHL2 = True
    end if
    
    if bHL then
        TextOut = TextOut & " " & HLStyle & WordArr(i) & "</span>"
    elseif bHL2 then
        TextOut = TextOut & " " & HLStyle2 & WordArr(i) & "</span>"
    else
        TextOut = TextOut & " " &  WordArr(i)
    end if
next
HighLight = TextOut
end function%>

And this is my postit.asp

Code:
<% @LANGUAGE = VBScript %>
<% Option explicit %>
<!--#include virtual="/chrisdev/incfuncstext.asp"-->
<%
Response.Expires = 0

Dim objConn, objRS, strQuery, tempvar, strQuery2, keyword, bAll, textout
Dim strConnection
dim i_id, s_search, pagein, searchterm,t,i,TempoJusto,StartNum,HighlightedText,TextIn,txtToHighlight
keyword = objRS("Keywords")' String to search in.

i_id = cint(request.querystring("ID"))
s_search = cstr(request.querystring("search"))
Set objConn = Server.CreateObject("ADODB.Connection")
strConnection = "DSN=CAGeHelp;Database=Front;"
strConnection = strConnection  & "UID=sa;PWD=;"
objConn.Open strConnection
strQuery = "SELECT Keywords, Helpinfo, Group, Subgroup FROM helpfile "
strQuery = strQuery & " WHERE HN = " & i_id 
strQuery = strQuery & " ORDER BY Keywords"
Set objRS = objConn.Execute(strQuery)

dim SearchString,SearchChar

SearchChar = s_search  

'textout = InStr(1, SearchString, SearchChar)   
response.write PM
bAll = true
textout = HighLight(s_search,keyword,bAll)
response.write textout
%>
<script language="JavaScript">
<!--
function FP_preloadImgs() {//v1.0
 var d=document,a=arguments; if(!d.FP_imgs) d.FP_imgs=new Array();
 for(var i=0; i<a.length; i++) { d.FP_imgs[i]=new Image; d.FP_imgs[i].src=a[i]; }
}

function FP_swapImg() {//v1.0
 var doc=document,args=arguments,elm,n; doc.$imgSwaps=new Array(); for(n=2; n<args.length;
 n+=2) { elm=FP_getObjectByID(args[n]); if(elm) { doc.$imgSwaps[doc.$imgSwaps.length]=elm;
 elm.$src=elm.src; elm.src=args[n+1]; } }
}

function FP_getObjectByID(id,o) {//v1.0
 var c,el,els,f,m,n; if(!o)o=document; if(o.getElementById) el=o.getElementById(id);
 else if(o.layers) c=o.layers; else if(o.all) el=o.all[id]; if(el) return el;
 if(o.id==id || o.name==id) return o; if(o.childNodes) c=o.childNodes; if(c)
 for(n=0; n<c.length; n++) { el=FP_getObjectByID(id,c[n]); if(el) return el; }
 f=o.forms; if(f) for(n=0; n<f.length; n++) { els=f[n].elements;
 for(m=0; m<els.length; m++){ el=FP_getObjectByID(id,els[n]); if(el) return el; } }
 return null;
}
// -->
</script>
<HTML>
<BODY onload="FP_preloadImgs(/*url*/'button2D.jpg',/*url*/'button2E.jpg')">
<font face="Arial" size="2">
<a href="default.asp">
<img border="0" id="img1" src="button2C.jpg" height="20" width="100" alt="Back to search" onmouseover="FP_swapImg(1,0,/*id*/'img1',/*url*/'button2D.jpg')" onmouseout="FP_swapImg(0,0,/*id*/'img1',/*url*/'button2C.jpg')" onmousedown="FP_swapImg(1,0,/*id*/'img1',/*url*/'button2E.jpg')" onmouseup="FP_swapImg(0,0,/*id*/'img1',/*url*/'button2D.jpg')" fp-style="fp-btn: Border Left 1" fp-title="Back to search"></a></font><p>
<font face="Arial" size="2">Help Topic Information <BR>&nbsp;</font></p>
<table border="0" width="100%" cellspacing="3" cellpadding="2">
	<tr>
		<td width="114"><font face="Arial" size="2">Keywords</font></td>
		<td><font face="Arial" size="2"><% Response.Write objRS("Keywords") %></font></td>
	</tr>
	<tr>
		<td width="114"><font face="Arial" size="2">Group </font></td>
		<td><font face="Arial" size="2"><% Response.Write objRS("Group") %>
		</font>
		</td>
	</tr>
	<tr>
		<td width="114"><font face="Arial" size="2">Subgroup</font></td>
		<td><font face="Arial" size="2"><% Response.Write objRS("SubGroup") %></font></td>
	</tr>
	<tr>
		<td width="114"><font face="Arial" size="2">Help Information
</font></td>
		<td><font face="Arial" size="2"><% Response.Write objRS("Helpinfo") %></font></td>
	</tr>
</table>
<p><font face="Arial" size="2"><BR>
<%While Not objRS.EOF%>
<BR><BR>  
<% Response.Write objRS("Helpinfo") 
objRS.MoveNext
Wend
objRS.close
objConn.close
Set objRS = Nothing
Set objConn = Nothing%>
</font></p>
</BODY>
</HTML>
 
you don't need to call it. it's part of my original function

Code:
    if PunctMarks(right(WordArr(i),1),"[.,]") then    'check for full stop or comma

it's in the code because if the word you are looking to highlight is at the end of a line (with a comma or full stop(period US)) it will miss getting matched.

paste this code into your included file
Code:
function PunctMarks(strIn, Pattern)
dim objRE
set objRE = New RegExp 
objRE.pattern = Pattern

PunctMarks = objRE.Test(strIn)

end function



Chris.

Indifference will be the downfall of mankind, but who cares?
A website that proves the cobblers kids adage.
Nightclub counting systems

So long, and thanks for all the fish.
 
i correct myself i have got it working thank you so much for you help and a very shiny star for your help
 

what about if the searched work appears in a link, it throws off the link.

test your scripts.

if you search website and in the page one if it's apearances is in <a href="./websitetest">demo 1</a>

that becomes
<a href="./<span style='color:red;font-weight:bold;background-color:#FFFFCC'>website</span>test">demo1</a>

now the wrong thing is highlighted and a broken link is created any thoughts?

LikeThisName <- ?
 
A simple way around it would be to add a check into the loop to set a flag if the array element contains either <a or href= then skip the highlight section, run a check at the bottom for the element being </a> and reset the flag.
that should then skip any links.

added lines in blue.
Code:
bHL2 = False
[blue]
if lcase(WordArr(i)) = "<a" then bNoHL = true
   if not bNoHL then
[/blue]
    if WordArr(i) = TextToHighlight then bHL = True ' highlight this word 

[blue]
if lcase(WordArr(i)) = "</a>" then bNoHL = false
[/blue]
next

not forgetting to dim bNoHL of course

BTW didn't test this just thinking at the keyboard


Chris.

Indifference will be the downfall of mankind, but who cares?
A website that proves the cobblers kids adage.
Nightclub counting systems

So long, and thanks for all the fish.
 
that deserves a fifth star, i was thinking of those lines, and was going to post a solution to my question but you beat me to it.

LikeThisName <- ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top