Page 1 of 1

str_replace() with uncommon characters

Posted: Mon Sep 06, 2010 7:13 pm
by swan52
I have a database table that contains names of people and I created a search function for easy access to this list. Unfortunately characters such as ø, ö and ü do not match their corresponding 'like' characters, (o or u) so I created a field soley for the purpose of searching, using a string such as $search_name = str_replace("ø","o",$search_name); to replace the offending characters so that names could be found easily on a standard English keyboard.
Obviously this didn't work or I wouldn't be posting it here and I have tried inputting the character code in various formats to no avail, so my question is what am I doing wrong?

Re: str_replace() with uncommon characters

Posted: Mon Sep 06, 2010 8:33 pm
by yacahuma
is your database utf-8?

Re: str_replace() with uncommon characters

Posted: Mon Sep 06, 2010 8:50 pm
by requinix
LIKE and MATCH AGAINST will work fine but only if everything is in the same encoding:
- Input from the web - use a <meta> or a PHP header()
- Database connection - for MySQL use SET NAMES
- Database tables - specify in the CREATE TABLE or set the database default

Re: str_replace() with uncommon characters

Posted: Tue Sep 07, 2010 5:30 am
by swan52
yacahuma wrote:is your database utf-8?
No, all the database and varchar fields are in the default latin1_swedish_ci charset.

I'm not sure I've described this correctly, I was trying to make a secondary field that contains both the first and last names from the table, but with letters such as ø omitted and replaced by their simpler counterparts so that names like for example, 'Bjørn' or 'Björn' would get a hit from just typing 'bjorn'.
Obviously my solution was a fairly simplistic fix and were people to search with those characters I would have to apply a similar letter replacement to the search function too, but as it just plain doesn't seem to work, I'm kinda curious to know as to why this is happening.

Re: str_replace() with uncommon characters

Posted: Tue Sep 07, 2010 8:56 am
by requinix
It's happening because *cough* not everything is in the same encoding.

Re: str_replace() with uncommon characters

Posted: Tue Sep 07, 2010 9:55 am
by yacahuma
you should consider the exercise of moving to utf8. But you have to be carefull anyway since utf8 support on php is not expected until php6. Until then you depend on external libs.

Re: str_replace() with uncommon characters

Posted: Tue Sep 07, 2010 12:32 pm
by swan52
yacahuma wrote:you should consider the exercise of moving to utf8. But you have to be carefull anyway since utf8 support on php is not expected until php6. Until then you depend on external libs.
Ok thanks, I tried it before but I think I must've selected the wrong collation because I was having earlier issues with charsets, hence keeping to the default but since I've changed it everything works perfectly now. Apologies for seeming a little naive but I've been losing quite a lot of hair over this 'small' issue so thanks again.