I have two strings:
The first string with dynamic content:
@string = “this is the example of the text”
The second one includes search words:
@search = “example text”
How can I do that it prints the search words with bold?
Result:
this is the example of the example
Mark T. wrote:
Result:
this is the example of the example
Hi,
You can use String#gsub to find and replace the words you want. The
example you have given could be solved by the following script:
Start code
def highlight(text,search_string)
keywords = search_string.squeeze.strip.split(" “).compact.uniq
matcher = Regexp.new( ‘(’ + keywords.join(”|") + ‘)’ )
highlighted = text.gsub(matcher) { |match| “#{match}” }
return highlighted
end
text = “This is an example of the text.”
search = “example text”
puts highlight(text,search)
=> This is an example of the text.
End code
There are probably plenty of opportunities to optimize and add defensive
coding, such as stripping out regexp-meaningful characters from the
search string, but this should be a start.
Cheers,
Wes ‘Narnach’ Oldenbeuving
Thanks for your answer.
I have now tried, but it only sets the first search word bold not the
others.
I´m a newbie for this and don´t know what the problem can be…
/regards,
mark
Mark T. wrote:
Thanks for your answer.
I have now tried, but it only sets the first search word bold not the
others.
I´m a newbie for this and don´t know what the problem can be…
/regards,
mark
I want to correct myself. It works for every search words, but they have
to be directly after each other.
It works for example:
text = “This is an example text.”
search = “example text”
here are “example text” bold
but not for:
text = “This is an example of the text.”
search = “example text”
here is only “example” bold
Any idea?
Regards,
Mark
search = “example text”
here is only “example” bold
Any idea?
I can not reproduce the behaviour you are getting using my highlight
method. Can you post the relevant parts of your (test) code you use to
get these results?
I added more sentences and another search string and they produce the
desired behaviour. I’m testing with ruby 1.8.6p111.
Code
def highlight
…
text = “This is an example of the text.”
text2 = “This is an example text.”
text3 = “This is another text used as an example for testing.”
search = “example text”
search2 = “test text”
puts highlight(text,search)
=> This is an example of the text.
puts highlight(text2,search)
=> This is an example text.
puts highlight(text3,search2)
=> This is another text used as an example for testing.
End code
Cheers,
Wes
Well I´m using it together with some ajax search from a database.
Here´s my code:
search_controller.rb
def live_search
$KCODE = ‘latin1’
@code = $KCODE
a1 = “%”
a2 = “%”
@searchstring_name_temp = params[:searching]
@searchstring_name = @searchstring_name_temp.rstrip.gsub(" “, “%’
AND name LIKE '%”)
@searchstring_sku_temp = params[:searching]
@searchstring_sku = @searchstring_sku_temp.rstrip.gsub(” ", “%’ AND
sku LIKE '%”)
@results = Product.find_by_sql(“SELECT * FROM products WHERE name
LIKE’”+a1+@searchstring_name+a2+"’ OR sku
LIKE’"+a1+@searchstring_sku+a2+"’ order by name asc LIMIT 0,50;")
@results = Product.find_by_sql(:all, :conditions => [ "name LIKE
?", a1+@searchstring+a2], :limit => 50)
@number_match = @results.length
render(:layout => false)
end
def highlight(text,search_string)
keywords = search_string.squeeze.strip.split(" “).compact.uniq
matcher = Regexp.new( ‘(’ + keywords.join(”|") + ‘)’ )
highlighted = text.gsub(matcher) { |match| “#{match}” }
return highlighted
end
live_search.rb
<%= @searchstring %>
<% if @results.empty? %>
not found!
<% else %>
found <%= @number_match %> time(s)!
<% @text = “This is an example of the text.”
@search = “example text” %>
<%= highlight(@text,@search) %>
sku |
name |
<% @results.each do |row| %>
<%= highlight(row.sku.capitalize,@searchstring_name_temp) %>
|
<%= highlight(row.name.capitalize,@searchstring_name_temp)
%>
|
<% end %>
<% end %>