convert accented characters to its equivalent ascii chars

Discussion in 'PHP' started by ahdsan, Sep 22, 2009.

  1. #1
    I am displaying state names from the db.
    one of the state name is "Québec", it has a link which says "/state/Québec/", it should say "/state/Quebec".
    I tried all the functions, all my best , but i don't knnow why its not converting.
    i used :
    1) str_replace("é","e",$string)
    2) got a function, remove accented chars
    return strtr($string, "ŠŒŽšœžŸ¥µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿ","SOZsozYYuAAAAAAACEEEEIIIIDNOOOOOOUUUUYsaaaaaaaceeeeiiiionoooooouuuuyy");
    when i compare the string
    $string=="Québec", it returns false, but the when i echo the string
    it says Québec, i am tooooooo confused.
    i dono which way to move now ?
    any help pleaseeeeee
     
    ahdsan, Sep 22, 2009 IP
  2. pneulameiro

    pneulameiro Peon

    Messages:
    440
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    in the database the string is recorded as "Québec" or as the html equivalent Que-&-accute;bec ?
     
    pneulameiro, Sep 22, 2009 IP
  3. lp1051

    lp1051 Well-Known Member

    Messages:
    163
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #3
    Hi,
    there could be conflict with encoding of your database records with the format of your PHP file. Are they both the same? Because on plain PHP file, encoded in ANSI it works. But as soon as your string is ISO something and your PHP file is UTF-8, it doesn't translate the characters right. You may check that or apply iconv() prior to strtr()...
     
    lp1051, Sep 22, 2009 IP
  4. ahdsan

    ahdsan Member

    Messages:
    74
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #4
    in database it is written as 'Québec'
    Also,
    i echoed the encoding of the string using mb_detect_encoding and it gave me UTF-8
    how to use iconv() function ??
     
    ahdsan, Sep 22, 2009 IP
  5. lp1051

    lp1051 Well-Known Member

    Messages:
    163
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #5
    Ok, and what if you use the function from here? Does it do anything? I see you have some few characters more, but you can easily expand it.
     
    lp1051, Sep 22, 2009 IP
  6. ahdsan

    ahdsan Member

    Messages:
    74
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #6
    when i use that function it gives me
    QuA(a square with FFDD)bec
     
    ahdsan, Sep 22, 2009 IP
  7. lp1051

    lp1051 Well-Known Member

    Messages:
    163
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #7
    Ok, then I'm pretty sure there's encoding problem of your PHP file with encoding in your DB. Check what is encoding of your column (that stores 'Quebec') in DB. As you said it should be utf-8 (probably utf-8 general). Then check that your PHP function/class for DB connection sets proper encoding - e.g. mysql_query("SET NAMES 'utf8'"); after you connect and select DB. Then copy once again the function from PHP manual or the one you have, and ensure you save the PHP file as utf-8. After that you should get it. Let us know...
     
    lp1051, Sep 23, 2009 IP