View Full Version : bold the text found
charon
03-14-2003, 01:06 PM
hi,
I really can't think of how could I replace whatever text which found with <b> (bold). Please don't misundertand what I want, below is the explaination.
strSearch = "Data"
Once the text is found (case insensitive), "bold" it.
For instance, the text that I'm going to match is "Data",
1.) if the first content has "data" just bold it data , not
Data
2.) If the content has "DATA", just bold DATA , not
Data
Please advice, thanks. Currently I only manage to replace whatever text found with the strSearch. As long as there has "Data" matched, it will bold it with "Data", but this is not what i want.
BigDaddy
03-14-2003, 02:17 PM
Regular Expressions, my friend.
Perhaps our resident regex guru, whammy can give you a good one to use, but this would get you started:
http://www.4guysfromrolla.com/webtech/RegularExpressions.shtml
It's got some good stuff on it about searching thru text for specific things.
head8k
03-14-2003, 05:10 PM
Yes, regualr expressions are the key here. The link posted above is a very good place to start learning about them but it's always nice to see an example so here you go. Bear in mind you will need to be running a later version of Microsoft's scripting engine to use the regular expression object: IIS 5 / ASP v3 is fine.
<%
strTest = "this is a string containing the word Data and others. Data again. lowercase data. blah blah blah."
strSearch = "Data"
strNewString = "<b>"&strSearch&"</b>"
Set objRegExp = New RegExp
objRegExp.Pattern = strSearch
objRegExp.IgnoreCase = False
objRegExp.Global = True
strReplaced = objRegExp.Replace(strTest, strNewString)
Response.Write strReplaced
Set objRegExp = Nothing
%>
charon
03-15-2003, 02:06 AM
so sorry to say that this is not what I want....as I emphasize in the example I given,
strSearch = "Data"
If the content has match "data" just bold it data , not Replace with
Data
2.) If the content has "DATA", just bold DATA , not Replace with
Data
What I want is bold the word found without changing the text's case This is very important when displaying the result.
If the content is :
This is my data record, then just bold the data", don't replace with Data
Base on the code given by head8k, the result displayed will be :
This is my Data record, I don't want this.
head8k
03-15-2003, 09:52 AM
Did you try the example I posted above? It does exactly what you ask for (if I can understand you correctly).
You could always try setting the objRegExp.IgnoreCase to True which might acheive the effect you are after.
charon
03-16-2003, 06:45 AM
Dear Head8k, well, I tried oredi, this is the result:
this is a string containing the word and others. Data again. lowercase data. blah blah blah.
what I want is this:
this is a string containing the word and others. Data again. lowercase data . blah blah blah.
you can try.....hope you can help.
Thanks so much!
david7777
03-17-2003, 11:20 AM
This is a really long way around, but you might be able to do it like this:
Dim tmpString
Dim word = "data"
// Contains the length of the search word - ie: "data" - in this case, 4
Dim wordLength = len(word)
Dim str= "this is a string containing the word and others. Data again. lowercase data. blah blah blah. "
// 'Find the index of the word "data"
indx = InStr(str,word) // In this case - first occurance is 50
// 'Now make a new string of everything before the word
tmpString = mid(str,1,indx-1)
// 'This will be: "this is a string containing the word and others. "
// 'Add bold tags
tmpString = tmpString + "<b>"
// 'This will be: "this is a string containing the word and others. <b>"
tmpString = tmpString + mid(str,indx,indx+wordLength)
// 'This will be: "this is a string containing the word and others. <b>Data"
tmpString = tmpString + "</b>"
// 'This will be: "this is a string containing the word and others. <b>Data</b>"
// 'Now do all this in a loop to bold all the matching words in the text...
I havent tried this, so i dont know if it will work - just an idea...
AS I said, its a long way around, but it might help or give ideas or something...
charon
03-17-2003, 11:24 AM
ok, thanks, will try and will keep u inform.
david7777
03-17-2003, 01:48 PM
Try this code out - I originally made it in ASP.net, and then converted,
so i will give you both in case my conversion was bad...
The ASP.net one definately works properly, and probably the converted one too...
ASP.NET:
' searchWord: Word to search for
' text2search: text to search through
' beforeHTML: HTML to put before each occurance of the word to search for
' afterHTML: HTML to put after each occurance of the word to search for
' returns the edited result
Function boldWords(searchWord as String, text2search as String, beforeHTML as String, afterHTML as String) as String
dim tmpString as String
Dim indx as Integer
Dim indx2 as Integer = 0
Dim wordLength as Integer = len(searchWord) 'Contains the length of the search word - ie: "data" - in this case, 4
Dim wholeLength as Integer = len(beforeHTML & searchWord & afterHTML) ' The length of the search word as well as the html code before and after it
do
'Find the index of the word
indx = InStr(indx2 + 1, LCase(text2search), LCase(searchWord)) ' In this case - first occurance is 23
if indx = 0 ' If no match, exit
exit do
end if
tmpString = mid(text2search,1,indx-1)
'Add html tags before the word
tmpString = tmpString + beforeHTML
'Add the word in original case
tmpString = tmpString + mid(text2search,indx,wordLength)
'Add html tags after the word
tmpString = tmpString + afterHTML
'Add the rest of the text to the end to continue looking
tmpString = tmpString + mid(text2search,indx+wordLength,Len(text2search))
indx2 = indx + wholeLength
text2search = tmpString
loop
return tmpString
End Function
' Usage:
dim str as String = "This is a string with data of all kinds in it... The DATA is upper case, or data is lower case, or Data, DaTa, dAtA, DATa, dATA, or daTaS"
laresponse.write(boldWords("data",str, "<font color=red><b>","</b></font>"))
Converted to Classic ASP:
' searchWord: Word to search for
' text2search: text to search through
' beforeHTML: HTML to put before each occurance of the word to search for
' afterHTML: HTML to put after each occurance of the word to search for
' returns the edited result
Function boldWords(searchWord, text2search, beforeHTML, afterHTML)
dim tmpString
Dim indx
Dim indx2 = 0
Dim wordLength = len(searchWord) 'Contains the length of the search word - ie: "data" - in this case, 4
Dim wholeLength = len(beforeHTML & searchWord & afterHTML) ' The length of the search word as well as the html code before and after it
do
'Find the index of the word
indx = InStr(indx2 + 1, LCase(text2search), LCase(searchWord)) ' In this case - first occurance is 23
if indx = 0 ' If no match, exit
exit do
end if
tmpString = mid(text2search,1,indx-1)
'Add html tags before the word
tmpString = tmpString + beforeHTML
'Add the word in original case
tmpString = tmpString + mid(text2search,indx,wordLength)
'Add html tags after the word
tmpString = tmpString + afterHTML
'Add the rest of the text to the end to continue looking
tmpString = tmpString + mid(text2search,indx+wordLength,Len(text2search))
indx2 = indx + wholeLength
text2search = tmpString
loop
boldWords = tmpString
End Function
'Usage:
dim str = "This is a string with data of all kinds in it... The DATA is upper case, or data is lower case, or Data, DaTa, dAtA, DATa, dATA, or daTaS"
response.write(boldWords("data",str, "<font color=red><b>","</b></font>"))
This will give the following (Cant use bold, so im using a bigger font):
This is a string with data of all kinds in it... The DATA is upper case, or data is lower case, or Data, DaTa, dAtA, DATa, dATA, or daTaS
Let me know if something is wrong or if you are having trouble...
:cool:
charon
03-18-2003, 01:39 AM
will let know after trying.:))
whammy
03-18-2003, 01:57 AM
Actually, to be honest with you I was just about to repost what Big Daddy already posted!
That is what you need to learn. :)
And that particular article is pretty good... that is a GREAT collection of articles, and one of the first that really taught me about regex's!
whammy
03-18-2003, 02:02 AM
P.S. Yeck, I can't stand VB.NET. ;)
I'd much rather do that kind of thing in C#... still learning, but I've got some regex's working just fine in C#, it's a lot less code. :D
charon
03-18-2003, 02:51 AM
Dear All,
with minor changes, the Code really work as what I want the text to be and It is easy to understand.
Thanks thanks a lot.
allida77
03-18-2003, 03:12 AM
P.S. Yeck, I can't stand VB.NET.
Why dont you like vb.net?
david7777
03-18-2003, 06:59 AM
charon, as you said, you have what you were looking for, but this question is to the people who have suggested other solutions... Just for my own clarification.
Will regular expressions help what charon is looking for? Sure - regular expressions may have their place in this situation, but I dont think it is the solutution to the problem...
Looking at the suggested solution of head8k, If there is the text:
"This is a string with data of all kinds in it... The DATA is upper case, or data is lower case, or Data, DaTa, dAtA, DATa, dATA, or daTaS"
Will it turn out as:
"This is a string with data of all kinds in it... The DATA is upper case, or data is lower case, or Data, DaTa, dAtA, DATa, dATA, or daTaS"
Or will it turn out as:
"This is a string with data of all kinds in it... The data is upper case, or data is lower case, or data, data, data, data, data, or dataS"
Notice that the casing of the second one has changed... That is why I gave charon the code like I did. charon wanted to prevent the case of the searched word to be changed. ie- he wanted the result of the first example above, not the second.
Please let me know if I am mistaken, or if there is a better / shorter way...
:cool:
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.