replace words to be bold for each search word

Discussion in 'Ruby' started by Mark Toth, Dec 30, 2007.

  1. Mark Toth

    Mark Toth Guest

    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 <b>example</b> of the <b>example</b>
    --
    Posted via http://www.ruby-forum.com/.
     
    Mark Toth, Dec 30, 2007
    #1
    1. Advertising

  2. Mark Toth

    Mark Toth Guest

    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

    --
    Posted via http://www.ruby-forum.com/.
     
    Mark Toth, Dec 30, 2007
    #2
    1. Advertising

  3. Mark Toth

    Mark Toth Guest

    Mark Toth 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
    --
    Posted via http://www.ruby-forum.com/.
     
    Mark Toth, Dec 30, 2007
    #3
  4. Mark Toth

    Mark Toth Guest

    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| "<b>#{match}</b>" }
    return highlighted
    end

    live_search.rb
    ================
    <%= @searchstring %>
    <% if @results.empty? %>
    not found!
    <% else %>
    found <b><%= @number_match %></b> time(s)!<BR>

    <% @text = "This is an example of the text."
    @search = "example text" %>
    <%= highlight(@text,@search) %>

    <table width=600 cellspacing="3">
    <td width="100"><h2>sku</td><td width="500"><h2><b>name</td>
    <% @results.each do |row| %>
    <tr>
    <td>
    <%= highlight(row.sku.capitalize,@searchstring_name_temp) %>
    </td>
    <td>
    <%= highlight(row.name.capitalize,@searchstring_name_temp)
    %>
    </td>

    <% end %>
    </table>
    <% end %>
    --
    Posted via http://www.ruby-forum.com/.
     
    Mark Toth, Dec 30, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. BerlinBrown
    Replies:
    6
    Views:
    4,840
  2. Lasse Edsvik

    replace words with bold words

    Lasse Edsvik, Oct 5, 2003, in forum: ASP General
    Replies:
    9
    Views:
    253
  3. Replies:
    0
    Views:
    97
  4. PerlFAQ Server
    Replies:
    0
    Views:
    153
    PerlFAQ Server
    Jan 26, 2011
  5. PerlFAQ Server

    FAQ 6.14 How do I process each word on each line?

    PerlFAQ Server, Apr 8, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    169
    PerlFAQ Server
    Apr 8, 2011
Loading...

Share This Page