Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Sep 2009
    Posts
    165
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Foreign character issue when using str_replace

    Hi, I'm trying to use str_replace to replace some German characters with their ASCII equivalents, like so:

    PHP Code:
    $description str_replace("%ö%""ö"$description); 
    And I also tried

    PHP Code:
    $description str_replace("%ö%""ö"$description); 
    I checked in the database and it doesn't show as the correct character- it shows as ö (and outputs as that)

    I need to be able to input / output German characters correctly in my form- I was hoping str_replace would do the job but it seems that it doesn't.

    My pages all have UTF-8 encoding in the header (I've heard that sometimes the issue is non-UTF encoding)

    I also made sure my mySQL db was UTF-8 encoded throughout, but still no joy.

    Any ideas?
    Last edited by galahad3; 01-24-2012 at 07:25 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    What charset is your database in?

  • #3
    Regular Coder
    Join Date
    Sep 2009
    Posts
    165
    Thanks
    16
    Thanked 0 Times in 0 Posts
    It *was* in Latin but I changed the whole database to UTF-8 (general) and tested again- it's still doing the same thing unfortunately...

    I made the table itself, each of the table columns (aside from the primary key which autoincrements), and the database all UTF-8 General.

    I also added this to my db connection include:

    PHP Code:
    mysql_set_charset('utf8'); 
    - and interestingly it now looks ok *in the database* record, but it still isn't outputting. I tried changing the output variable as follows, but it didn't work:

    PHP Code:
      $description $row["Description"];
      
    $descriptionclean htmlentities($descriptionENT_QUOTES); 
    ???
    Last edited by galahad3; 01-24-2012 at 10:28 PM.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Try adding the charset to the htmlentities: $descriptionclean = htmlentities($description, ENT_QUOTES, 'UTF-8');. Also, try to avoid using str_replace at all, I'm *quite* sure that str_replace is only an 8 bit handling for the characters, and you'd have to use the mbstring equivalent to deal with the multibyte charsets. From the sounds of it, you just want to display it so forcing the charset I'd expect would work without the need of strreplace at all.

  • Users who have thanked Fou-Lu for this post:

    galahad3 (01-25-2012)

  • #5
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,862
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    I also added this to my db connection include:

    PHP Code:
    mysql_set_charset('utf8'); 
    Execute the following query before all insert/fetch operations
    PHP Code:
    mysql_query("SET NAMES 'utf8'"); 
    and you don't need any str_replace() for this. See http://dev.mysql.com/doc/refman/5.0/...onnection.html
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #6
    Regular Coder
    Join Date
    Sep 2009
    Posts
    165
    Thanks
    16
    Thanked 0 Times in 0 Posts
    Thanks, that seems to work perfectly. I'll use that output across all the forms that need it.

    Thanks again to Fou-lu and Abduraooft.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •