Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Highlight a word from some text

Posted on 2016-08-01
16
60 Views
Last Modified: 2016-08-01
I have a page that displays text from a nvarchar (8000) field in my database as: <%=(report_results.Fields.Item("comments").Value)%>

I am also passing a 'keyword' from the previous page and I retrieve it with: request.form("keyword")  

I want to highlight the value I pass as keyword in the text that is displayed on the page.  Example:

I my keyword is "second"

I want the text to display as "This is the second time I try this"  or something of that nature. How can I do this ?

I am using SQL 2008 and ASP Classic
0
Comment
Question by:amucinobluedot
  • 8
  • 8
16 Comments
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 41738324
what you see in your page is generated and displayed as HTML. Hence, to highlight certain words based on certain actions, you need to change the HTML codes as well.

so, now the question is what is the container of that area to display your wordings? is it a DIV, etc? do you have the codes to post here?
0
 

Author Comment

by:amucinobluedot
ID: 41738332
Sure. This is the whole table where I have a repeated region showing the results.

 <table class="table table-striped table-bordered table-hover dataTables-example" >
                <thead>
                  <tr>
                    <th width="3%">Actions</th>
                    <th width="10%">User</th>
                    <th width="46%">Notes</th>
                    <th width="10%">Case number</th>
                    <th width="13%">Contact</th>
                    <th width="8%">Last updated</th>
                    <th width="10%">Case status</th>
                    </tr>
                </thead>
                <tbody>
                <% 
While ((Repeat1__numRows <> 0) AND (NOT report_results.EOF)) 
%>
                    <tr class="gradeX">
                      <td><a href="../cases/notes_index.asp?caseId=<%=(report_results.Fields.Item("caseid").Value)%>" target="_blank" class="btn btn-white btn-sm"><i class="fa fa-folder" text-navy></i></a></td>
                      <td><%=(report_results.Fields.Item("atty").Value)%></td>
                      <td><%=(report_results.Fields.Item("comments").Value)%></td>
                      <td><%=(report_results.Fields.Item("caseid").Value)%></td>
                      <td><%=(report_results.Fields.Item("Alien").Value)%></td>
                      <td><%= DoDateTime((report_results.Fields.Item("Lastupdate").Value), 2, 9225) %></td>
                      <td><%=(report_results.Fields.Item("casestatus").Value)%></td>
                    </tr>
                                    <% 
  Repeat1__index=Repeat1__index+1
  Repeat1__numRows=Repeat1__numRows-1
  report_results.MoveNext()
Wend
%>
                </tbody>
                <tfoot>
                  <tr>
               <th width="3%">Actions</th>
                    <th width="10%">User</th>
                    <th width="46%">Notes</th>
                    <th width="10%">Case number</th>
                    <th width="13%">Contact</th>
                    <th width="8%">Last updated</th>
                    <th width="10%">Case status</th>
                    </tr>
                    

                </tfoot>
              </table>

Open in new window


The field that will include the text that should highlight any matching keywords is: <%=(report_results.Fields.Item("comments").Value)%>

Let me know if you need anything else.
0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 41738335
is that the content of report_results.Fields.Item("comments").Value is plain text or already in HTML?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:amucinobluedot
ID: 41738337
It is HTML code. User saves the content from an HTML editor, this is an example:

<p><strong>Client is traveling to:</strong> <span style="color:#ff0000">This is an example of a note</span></p><p>From:<br />To:</p><p>Additional comments:&nbsp;</p>

Open in new window


This is another example where user doesn't do anything other than enter text in the editor

test note no styling

Open in new window

0
 
LVL 50

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 41738367
you probably can refer to this article:

Keyword highlighting in ASP
http://www.codeproject.com/Articles/610/Keyword-highlighting-in-ASP

then try something like:
<%=Highlight(report_results.Fields.Item("comments").Value, "second"  , "<font color=red><b>", "</b></font>"  )%>

Open in new window

0
 

Author Comment

by:amucinobluedot
ID: 41738400
I made a mistake and closed the question. Unfortunately it didn't work, it doesnt highlight the word  :$
0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 41738402
let me try and get back to you shortly..
0
 

Author Comment

by:amucinobluedot
ID: 41738404
Sounds good. Same result with other browsers
0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 41738407
simple test looks good:
<%


text = "<p><strong>Client is traveling to:</strong> <span style=""color:#ff0000"">This is an example of a note</span></p><p>From:<br />To:</p><p>Additional comments:&nbsp; whatever this is second ++ </p>"

response.write Highlight(text, "second"  , "<font color=red><b>", "</b></font>"  )

Function Highlight(strText, strFind, strBefore, strAfter)
	Dim nPos
	Dim nLen
	Dim nLenAll
	
	nLen = Len(strFind)
	nLenAll = nLen + Len(strBefore) + Len(strAfter) + 1

	Highlight = strText

	If nLen > 0 And Len(Highlight) > 0 Then
		nPos = InStr(1, Highlight, strFind, 1)
		Do While nPos > 0
			Highlight = Left(Highlight, nPos - 1) & _
				strBefore & Mid(Highlight, nPos, nLen) & strAfter & _
				Mid(Highlight, nPos + nLen)

			nPos = InStr(nPos + nLenAll, Highlight, strFind, 1)
		Loop
	End If
End Function

%>

Open in new window


>>Ok. For some reason it only highlited in red the first time I did it, then the results page keeps highlighting that one word.
Do you have a screenshot to share here?
0
 

Author Comment

by:amucinobluedot
ID: 41738416
Well I think the problem is that I copy/pasted the code. It is always using 'second' to highlight, I should change it to the value of the form

<%=Highlight(report_results.Fields.Item("comments").Value, "second"  , "<font color=red><b>", "</b></font>"  )%>

Open in new window


How can I replace 'second'  for <%=request.form("keyword")%>   ?
0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 41738419
alright, I think you got the problem resolved.
0
 

Author Comment

by:amucinobluedot
ID: 41738421
Not 100% ... just need a little help with the syntax to use the form value instead of the hard coded value of 'second'
0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 41738429
alright, so try this:
<%=Highlight(report_results.Fields.Item("comments").Value, request.form("keyword")  , "<font color=red><b>", "</b></font>"  )%>

Open in new window

0
 

Author Comment

by:amucinobluedot
ID: 41738433
Awesome !  thanks so much. all set here.
0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 41738434
coool, glad that it works!
0
 

Author Comment

by:amucinobluedot
ID: 41738435
It does, perfectly. Have a good night!
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
website rewamp 5 42
Need help with a Stored Proc on Sql Server 2012 4 28
Company website 6 30
WordPress 8 27
Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question