Getting ’ instead of an apostrophe(') in PHP

匆匆过客 提交于 2019-12-17 04:26:10

问题


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 should be:

"It’s Getting the Best of Me"

I'm getting this data from this url.


回答1:


To convert to HTML entities:

<?php
  echo mb_convert_encoding(
    file_get_contents('http://www.tvrage.com/quickinfo.php?show=Surviver&ep=20x02&exact=0'),
    "HTML-ENTITIES",
    "UTF-8"
  );
?>

See docs for mb_convert_encoding for more encoding options.




回答2:


Make sure your html header specifies utf8

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

That usually does the trick for me (obviously if the content IS utf8).

You don't need to convert to html entities if you set the content-type.




回答3:


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.




回答4:


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');



回答5:


If you're here because you're experiencing issues with junk characters in your WordPress site, try this:

  1. Open wp-config.php

  2. Comment out define('DB_CHARSET', 'utf8') and define('DB_COLLATE', '')

    /** MySQL hostname */
    define('DB_HOST', 'localhost');
    
    /** Database Charset to use in creating database tables. */
    //define('DB_CHARSET', 'utf8');
    
    /** The Database Collate type. Don't change this if in doubt. */
    //define('DB_COLLATE', '');
    



回答6:


It sounds like you're using standard string functions on a UTF8 characters (’) that doesn't exist in ISO 8859-1. Check that you are using Unicode compatible PHP settings and functions. See also the multibyte string functions.




回答7:


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;
?>



回答8:


I looked at the link, and it looks like UTF-8 to me. i.e., in Firefox, if you pick View, Character Encoding, UTF-8, it will appear correctly.

So, you just need to figure out how to get your PHP code to process that as UTF-8. Good luck!




回答9:


try this :

html_entity_decode(mb_convert_encoding(stripslashes($text), "HTML-ENTITIES", 'UTF-8'))



回答10:


We had success going the other direction using this:

mb_convert_encoding($text, "HTML-ENTITIES", "ISO-8859-1");



回答11:


Just try this

if $text contains strange charaters do this:

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

and you are done..




回答12:


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")));



回答13:


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" />



回答14:


You Should check encode encoding origin then try to convert to correct encode type.

In my case, I read csv files then import to db. Some files displays well some not. I check encoding and see that file with encoding ASCII displays well, other file with UTF-8 is broken. So I use following code to convert encoding:

if(mb_detect_encoding($content) == 'UTF-8') {
    $content = iconv("UTF-8", "ASCII//TRANSLIT", $content);
    file_put_contents($file_path, $content);
} else {
    $content = mb_convert_encoding($content, 'UTF-8', 'UTF-8');
    file_put_contents($file_path, $content);
}

After convert I push the content to file then process import to DB, now it displays well in front-end



来源:https://stackoverflow.com/questions/2292004/getting-%c3%a2%e2%82%ac-instead-of-an-apostrophe-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!