61

我尝试将文本转换为 utf8 或从 utf8 转换,这似乎没有帮助。

我越来越:

"It’s Getting the Best of Me"

它应该是:

"It’s Getting the Best of Me"

我正在从这个 url 获取这些数据。

4

16 回答 16

91

要转换为 HTML 实体:

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

于 2010-02-18T20:46:49.317 回答
33

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.

于 2010-03-24T06:21:34.363 回答
12

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.

于 2010-02-18T20:47:36.350 回答
9
于 2013-09-11T14:41:13.190 回答
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', '');
    
于 2012-06-21T18:08:06.207 回答
3

听起来您在ISO 8859-1中不存在的 UTF8 字符 (') 上使用标准字符串函数。检查您是否使用了与Unicode 兼容的PHP 设置和函数。另请参见多字节字符串函数。

于 2010-02-18T20:41:04.567 回答
3

Just try this

if $text contains strange charaters do this:

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

and you are done..

于 2016-02-04T05:47:34.293 回答
2

We had success going the other direction using this:

mb_convert_encoding($text, "HTML-ENTITIES", "ISO-8859-1");
于 2014-03-20T22:09:06.480 回答
2

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;
?>
于 2016-08-18T15:00:26.960 回答
1

try this :

html_entity_decode(mb_convert_encoding(stripslashes($text), "HTML-ENTITIES", 'UTF-8'))
于 2013-10-17T15:22:03.533 回答
1

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")));
于 2017-04-26T17:46:45.343 回答
1

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

于 2019-08-26T02:55:01.623 回答
1

If none of the above solutions work:

In my case I noticed that the single quote was a different style of single quote. Instead of ' my data had a ’. Notice the difference in the single quote? So I simply wrote a str_replace to replace it and it fixed the problem. Probably not the most elegant solution but it got the job done.

$string= str_replace("’&quot;,"'",$string);
于 2021-01-29T15:06:27.603 回答
0

我查看了链接,对我来说它看起来像 UTF-8。即,在 Firefox 中,如果您选择查看、字符编码、UTF-8,它将正确显示。

因此,您只需要弄清楚如何让您的 PHP 代码将其处理为 UTF-8。祝你好运!

于 2010-02-18T20:40:27.097 回答
0

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" />
于 2013-02-15T10:33:40.783 回答
0

If nothing works try this mb_convert_encoding($elem->textContent, 'UTF-8', 'utf8mb4');

于 2020-12-11T16:04:58.493 回答