str_replace can't find special characters

Discussion in 'PHP' started by dizyn, Dec 23, 2012.

  1. #1
    I have some text saved in database and i want to replace few srtings in it

    For example i have text in database:

    "Översikt: Hotell Strand Palace är beläget mitt i Londons centrum"

    I want to repalce "Översikt" with "Hello" using str_replace but it does not work.

    When i echo it shows me: "
     
    dizyn, Dec 23, 2012 IP
  2. ratan1980

    ratan1980 Member

    Messages:
    46
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    28
    #2
    i could find replacement for diamond in html special characters but could not find for diamond with a question mark inside
     
    ratan1980, Dec 23, 2012 IP
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #3
    most likely you have a character encoding issue between the database and your PHP string. The extended character for Ö is different in ISO-8859-1, UTF-8, Windows-1252, and of course the latin1_swedish_ci that is the default in mySQL...

    You need to make sure your PHP code and your database are using the same encoding.
     
    deathshadow, Dec 24, 2012 IP
    zied.ho likes this.
  4. rstoll

    rstoll Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #4
    Make also sure that the HTML page supports your character set, the Ö respectively
     
    rstoll, Dec 26, 2012 IP
  5. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #5
    I always try to keep it a habit to use MB functions when dealing with non Latin languages. Assuming you got utf-8 encoded text:

    
    <?php
    // Set your encoding (incase you need to change it)
    mb_internal_encoding("UTF-8");
    mb_regex_encoding("UTF-8");
    
    $text = "Översikt: Hotell Strand Palace är beläget mitt i Londons centrum";
    $englishText = mb_ereg_replace('Översikt','Hello', $text);
    
    echo $text . '<br />' . $englishText . '<br />';
    
    PHP:
     
    ThePHPMaster, Dec 28, 2012 IP
  6. dizyn

    dizyn Active Member

    Messages:
    251
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #6
    I am using "Collation: latin1_swedish_ci" in mysql database. what should i use on my php and html page?

    thank you
     
    dizyn, Jan 1, 2013 IP
  7. BarbaraMibram

    BarbaraMibram Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #7
    I think str_replace Is a best option for you... And its working well.
     
    BarbaraMibram, Jan 2, 2013 IP
  8. dizyn

    dizyn Active Member

    Messages:
    251
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #8
    db is latin character set and web page is utf8 character set. that's why its creating problem for me. What if i convert my db table to utf-8 swedish? becaseu my language is swedish.

    thank you
     
    dizyn, Jan 2, 2013 IP
  9. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #9
    Instead of this
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    PHP:
    Use this
    <meta http-equiv="Content-Type" content="text/html; charset=charset=ISO-8859-1" />
    PHP:
     
    MyVodaFone, Jan 2, 2013 IP