Ideally, you shouldn't have HTML entities in the database. If you need them
in your HTML (and you don't, if you set an explicit encoding, except for
things like &<>) then you should add them outside the database.
If you do have "é" stored in the database, not as an entity, I believe
MySQL's "LIKE" will be accent-insensitive by default, unless you use
"COLLATE utf8_bin" (google for details).
Note that if you use "sub", you will only replace the first occurrence in
the string. You probably want "gsub".
And if you do something like "bléh".sub("é", "é") and it doesn't
replace the "é", the issue could be how the "é" is represented. In UTF-8,
accented characters can be represented either composed as a single glyph
("latin small letter e with acute") or decomposed as two glyphs: "latin
small letter e" + "combining acute accent". So if your string contains the
first type of é and your sub/gsub tries to replace the other type, it won't
work. You can normalize the string to ensure everything is composed or
decomposed, but it would be better not to have entities in the database.
On Sun, Jan 23, 2011 at 16:04, Mitchell Gould <[email protected]> wrote:
> I am trying to search my database which has product names with french
> accents. They are encoded using the html entity codes such as &eactue;
> etc.
>
> If a user enters a word with a french accent in the search box I must
> convert it to the html entity code so it can be found in the database.
>
> So I thought to use str.sub(pattern, replacement) => new_str
>
> However when I try this using product.sub('é','é') for example it
> results in the following:
>
> find(:all, :select => 'product_id, name', :order => "name", :conditions
> => ["name like ? and locale =?", "%#{product.sub('é','é')}%",
> I18n.locale])
>
> When I enter 'é' in the seach box I get the following:
>
> SELECT product_id, name FROM `product_descriptions` WHERE (name like
> '%é%' and locale ='en')
>
> so it does not replace the 'é' with '&eactue'
>
> But if I change the letter from 'é' to 'e' and do a search for 'e' I
> get the following:
>
> SELECT product_id, name FROM `product_descriptions` WHERE (name like
> '%é%' and locale ='en')
>
> so the replacement works.
>
> Can anyone explain why it won't work for the character with french
> accent?
>
> Thank you in advance.
> Mitch
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups
> "rails-i18n" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<rails-i18n%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/rails-i18n?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups
"rails-i18n" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rails-i18n?hl=en.