String highlighting problem (newbie)

M

Mark Toth

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
 
F

Frederick Cheung

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
 
C

Collins Richey

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>?

Brute force method?

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

Mark Toth

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(' ')
 
M

Mark Toth

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".
 
C

Collins Richey

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(' ')

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')
ix = a_nbr - 1
ar = a_str.split(' ')
ar[ix] = "<#{a_tag}>#{ar[ix]}</#{a_tag}>"
str = ar.join(' ')
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,
 
M

Mark Toth

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.
 
F

Frederick Cheung

Thanks for your answer.

I have now used the following for the 1.:

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

It works, BUT not for numbers. Any idea why it coesn=B4t work for =20
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 =20
less than 3 characters or so (check the doc) there are also stopwords =20=

etc...

Fred
About 2:

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

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


--=20
Posted via http://www.ruby-forum.com/.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,274
Messages
2,571,370
Members
48,062
Latest member
leehaan

Latest Threads

Top