# String highlighting problem (newbie)

**URL:** <https://rubytalk.org/t/string-highlighting-problem-newbie/43393>\
**Category:** ruby-talk\
**Created:** [29 December 2007 13:47 UTC](https://rubytalk.org/t/string-highlighting-problem-newbie/43393 "2007-12-29T13:47:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Toth](https://avatars.discourse-cdn.com/v4/letter/m/9fc29f/32.png) [@Mark\_Toth](https://rubytalk.org/u/Mark_Toth)\
**Post date:** [29 December 2007 13:47 UTC](https://rubytalk.org/t/string-highlighting-problem-newbie/43393/1 "2007-12-29T13:47:26Z")

</div>

I have two questions:

1. I have a string  
@string = "This is a long string".

How can I do to make two words "bold" or "italic" in this string, for  
example "is" and "long" shoudl be \<b\>?

2. I am searching in the mysql database with this code:

@results = Product.find(:all, :conditions =\> [ "name LIKE ?",  
"%"+@searchstring+"%"])

What if I want to search for many words in different order? I mean if I  
should searched for example: "string is a long this" in the string  
above?

Thanks,

Mark

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung1](https://avatars.discourse-cdn.com/v4/letter/f/ecc23a/32.png) [@Frederick\_Cheung1](https://rubytalk.org/u/Frederick_Cheung1)\
**Post date:** [29 December 2007 14:20 UTC](https://rubytalk.org/t/string-highlighting-problem-newbie/43393/2 "2007-12-29T14:20:24Z")

</div>

> I have two questions:
> 
> 1. I have a string  
> @string = "This is a long string".
> 
> How can I do to make two words "bold" or "italic" in this string, for  
> example "is" and "long" shoudl be \<b\>?

I would probably use gsub. But at the end of the day it depends on how the words to be bolded are determined.

> 2. I am searching in the mysql database with this code:
> 
> @results = Product.find(:all, :conditions =\> [ "name LIKE ?",  
> "%"+@searchstring+"%"])
> 
> What if I want to search for many words in different order? I mean if I  
> should searched for example: "string is a long this" in the string  
> above?

You need a full text search engine, for example the one in mysql, ferret, sphinx etc...

Fred

> **···**
>
> On 29 Dec 2007, at 13:47, Mark Toth wrote:
> 
> > Thanks,
> > 
> > Mark  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Collins\_Richey](https://avatars.discourse-cdn.com/v4/letter/c/eb8c5e/32.png) [@Collins\_Richey](https://rubytalk.org/u/Collins_Richey)\
**Post date:** [29 December 2007 18:00 UTC](https://rubytalk.org/t/string-highlighting-problem-newbie/43393/3 "2007-12-29T18:00:16Z")

</div>

Brute force method?

ar = @string.split(' ')  
ar[1] = "\<b\>#{ar[1]}\</b\>"  
ar[3] = "\<b\>#{ar[3]}\</b\>"  
@string = ar.join(' ')

> **···**
>
> On Dec 29, 2007 6:47 AM, Mark Toth \<mark.toth@telia.com\> wrote:
> 
> > I have two questions:
> > 
> > 1. I have a string  
> > @string = "This is a long string".
> > 
> > How can I do to make two words "bold" or "italic" in this string, for  
> > example "is" and "long" shoudl be \<b\>?
> 
> --  
> Collins Richey  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If you fill your heart with regrets of yesterday and the worries  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;of tomorrow, you have no today to be thankful for.

---

<div class="post-metadata">

**Author:** ![Mark\_Toth](https://avatars.discourse-cdn.com/v4/letter/m/9fc29f/32.png) [@Mark\_Toth](https://rubytalk.org/u/Mark_Toth)\
**Post date:** [29 December 2007 19:11 UTC](https://rubytalk.org/t/string-highlighting-problem-newbie/43393/4 "2007-12-29T19:11:53Z")

</div>

Thanks for your answer.

I have now used the following for the 1.:

a1 = "+"  
a2 = "\*"  
@searchstring = params[:searching].gsub(" ", "\* +")  
@results = Product.find\_by\_sql("SELECT \* FROM products WHERE MATCH  
(name,sku) AGAINST ('"+a1+@searchstring+a2+"' IN BOOLEAN MODE) order by  
name desc LIMIT 0,50;")

It works, BUT not for numbers. Any idea why it coesn´t work for numbers?  
If I have example: "this is 34" in the database. It works fine "this  
is", but as soon as I enter the "34" it shows no results.

About 2:

Brute force method? - What do you mean by this?

ar = @string.split(' ')  
ar[1] = "\<b\>#{ar[1]}\</b\>"  
ar[3] = "\<b\>#{ar[3]}\</b\>"  
@string = ar.join(' ')

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Mark\_Toth](https://avatars.discourse-cdn.com/v4/letter/m/9fc29f/32.png) [@Mark\_Toth](https://rubytalk.org/u/Mark_Toth)\
**Post date:** [29 December 2007 19:15 UTC](https://rubytalk.org/t/string-highlighting-problem-newbie/43393/5 "2007-12-29T19:15:10Z")

</div>

> About 2:
> 
> Brute force method? - What do you mean by this?
> 
> ar = @string.split(' ')  
> ar[1] = "\<b\>#{ar[1]}\</b\>"  
> ar[3] = "\<b\>#{ar[3]}\</b\>"  
> @string = ar.join(' ')

Also the string can be different, like "Blabla bla is a long string".

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Collins\_Richey](https://avatars.discourse-cdn.com/v4/letter/c/eb8c5e/32.png) [@Collins\_Richey](https://rubytalk.org/u/Collins_Richey)\
**Post date:** [29 December 2007 19:41 UTC](https://rubytalk.org/t/string-highlighting-problem-newbie/43393/6 "2007-12-29T19:41:27Z")

</div>

By brute force, I meant simply a quick and dirty operation  
1. split the string into components  
2. modify the components  
3. reassemble/join the components

If you have need to do this on a repetitive basis, you could develop a  
method to apply this logic, something like

def hilite(a\_str, a\_nbr, a\_tag='b')  
&nbsp;&nbsp;&nbsp;ix = a\_nbr - 1  
&nbsp;&nbsp;&nbsp;ar = a\_str.split(' ')  
&nbsp;&nbsp;&nbsp;ar[ix] = "\<#{a\_tag}\>#{ar[ix]}\</#{a\_tag}\>"  
&nbsp;&nbsp;&nbsp;str = ar.join(' ')  
&nbsp;&nbsp;&nbsp;return str  
end  
strx = "This is a long string"  
p strx  
strx = hilite(strx,2) # make word 2 bold  
p strx  
strx = hilite(strx,4,'i') # make word 4 italic  
p strx

This is still pretty brute force, since the method will break badly if  
you pass it the wrong elements - no error/bounds checking. You could  
modify it to scan for a particular word to hilite instead of using a  
word number.

Enjoy,

> **···**
>
> On Dec 29, 2007 12:11 PM, Mark Toth \<mark.toth@telia.com\> wrote:
> 
> > Brute force method? - What do you mean by this?
> > 
> > ar = @string.split(' ')  
> > ar[1] = "\<b\>#{ar[1]}\</b\>"  
> > ar[3] = "\<b\>#{ar[3]}\</b\>"  
> > @string = ar.join(' ')
> 
> --  
> Collins Richey  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If you fill your heart with regrets of yesterday and the worries  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;of tomorrow, you have no today to be thankful for.

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung1](https://avatars.discourse-cdn.com/v4/letter/f/ecc23a/32.png) [@Frederick\_Cheung1](https://rubytalk.org/u/Frederick_Cheung1)\
**Post date:** [30 December 2007 00:00 UTC](https://rubytalk.org/t/string-highlighting-problem-newbie/43393/7 "2007-12-30T00:00:40Z")

</div>

> Thanks for your answer.
> 
> I have now used the following for the 1.:
> 
> a1 = "+"  
> a2 = "\*"  
> @searchstring = params[:searching].gsub(" ", "\* +")  
> @results = Product.find\_by\_sql("SELECT \* FROM products WHERE MATCH  
> (name,sku) AGAINST ('"+a1+@searchstring+a2+"' IN BOOLEAN MODE) order by  
> name desc LIMIT 0,50;")
> 
> It works, BUT not for numbers. Any idea why it coesn´t work for numbers?  
> If I have example: "this is 34" in the database. It works fine "this  
> is", but as soon as I enter the "34" it shows no results.

Does it work for longer numbers? By default mysql won't index words less than 3 characters or so (check the doc) there are also stopwords etc...

Fred

> **···**
>
> On 29 Dec 2007, at 19:11, Mark Toth wrote:
> 
> > About 2:
> > 
> > Brute force method? - What do you mean by this?
> > 
> > ar = @string.split(' ')  
> > ar[1] = "\<b\>#{ar[1]}\</b\>"  
> > ar[3] = "\<b\>#{ar[3]}\</b\>"  
> > @string = ar.join(' ')
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Mark\_Toth](https://avatars.discourse-cdn.com/v4/letter/m/9fc29f/32.png) [@Mark\_Toth](https://rubytalk.org/u/Mark_Toth)\
**Post date:** [29 December 2007 20:01 UTC](https://rubytalk.org/t/string-highlighting-problem-newbie/43393/8 "2007-12-29T20:01:04Z")

</div>

Thanks, but one problem. I don´t now which word to replace. I mean in  
practicly I want to search for some words and then replace them with  
bold words.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
