Getting ’ instead of an apostrophe(') in PHP

前端 未结 14 927
慢半拍i
慢半拍i 2020-11-27 02:38

I\'ve tried converting the text to or from utf8, which didn\'t seem to help.

I\'m getting:

\"It’s Getting the Best of Me\"

It sho

相关标签:
14条回答
  • 2020-11-27 03:26

    use this

    <meta http-equiv="Content-Type" content="text/html; charset=utf8_unicode_ci" />
    

    instead of this

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    0 讨论(0)
  • 2020-11-27 03:27

    I know the question was answered but setting meta tag didn't help in my case and selected answer was not clear enough, so I wanted to provide simpler answer.

    So to keep it simple, store string into a variable and process that like this

    $TVrageGiberish = "It’s Getting the Best of Me";
    
    $notGiberish = mb_convert_encoding($TVrageGiberish, "HTML-ENTITIES", 'UTF-8');
    
    echo $notGiberish;
    

    Which should return what you wanted It’s Getting the Best of Me

    If you are parsing something, you can perform conversion while assigning values to a variable like this, where $TVrage is array with all the values, XML in this example from a feed that has tag "Title" which may contain special characters such as ‘ or ’.

    $cleanedTitle = mb_convert_encoding($TVrage->title, "HTML-ENTITIES", 'UTF-8');
    
    0 讨论(0)
  • 2020-11-27 03:31

    Your content is fine; the problem is with the headers the server is sending:

    Connection:Keep-Alive
    Content-Length:502
    Content-Type:text/html
    Date:Thu, 18 Feb 2010 20:45:32 GMT
    Keep-Alive:timeout=1, max=25
    Server:Apache/2.2.8 (Ubuntu) PHP/5.2.4-2ubuntu5.7 with Suhosin-Patch
    X-Powered-By:PHP/5.2.4-2ubuntu5.7
    

    Content-Type should be set to Content-type: text/plain; charset=utf-8, because this page is not HTML and uses the utf-8 encoding. Chromium on Mac guesses ISO-8859-1 and displays the characters you're describing.

    If you are not in control of the site, specify the encoding as UTF-8 to whatever function you use to retrieve the content. I'm not familiar enough with PHP to know how exactly.

    0 讨论(0)
  • 2020-11-27 03:31

    Just try this

    if $text contains strange charaters do this:

    $mytext = mb_convert_encoding($text, "HTML-ENTITIES", 'UTF-8');
    

    and you are done..

    0 讨论(0)
  • 2020-11-27 03:35

    if all seems not to work, this could be your best solution.

    <?php
    $content="It’s Getting the Best of Me";
    $content = str_replace("’", "&#39;", $content);
    echo $content;
    ?>
    

    ==or==

    <?php
    $content="It’s Getting the Best of Me";
    $content = str_replace("’", "'", $content);
    echo $content;
    ?>
    
    0 讨论(0)
  • 2020-11-27 03:35

    For fopen and file_put_contents, this will work:

    str_replace("&rsquo;", "'", htmlspecialchars_decode(mb_convert_encoding($string_to_be_fixed, "HTML-ENTITIES", "UTF-8")));
    
    0 讨论(0)
提交回复
热议问题