2010-02-18 104 views
43

我试过将文本转换为或来自utf8,这似乎没有帮助。在PHP中取代撇号(')

我越来越:

"It’s Getting the Best of Me" 

它应该是:

"It’s Getting the Best of Me" 

我从this url.

+2

我看到这个人的IM有时候还是从Mac给我发电子邮件。期待看到解决方案。 – 2010-02-18 20:37:28

+0

是的,我也在使用MAMP Pro测试Mac上的代码。 – Mint 2010-02-18 20:39:33

+0

如果这来自MySQL表格,这里给出的修复都不适用。 – 2017-12-28 00:20:51

回答

66

要转换为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" 
); 
?> 

mb_convert_encoding文档的更多编码选项。

+0

虽然我不知道如何让它在fopen上运行 – Mint 2010-02-19 04:11:40

+5

一旦你得到了你想要的文件的内容,然后把它作为第一个参数传递给'mb_convert_encoding()'。例如'$ text = fgets($ fp); $ html = mb_convert_encoding($ text,“HTML-ENTITIES”,“UTF-8”);' – Matthew 2010-02-19 04:46:06

+0

域名不再有效。 – mtness 2014-06-05 09:39:10

1

得到这个数据我看了一下链接,它看起来像UTF -8给我。即在Firefox中,如果您选择查看,字符编码,UTF-8,它将正确显示。

所以,你只需要弄清楚如何让你的PHP代码处理为UTF-8。祝你好运!

+0

尝试htmlspecialchars_decode – 2010-02-18 20:41:10

+0

Nop,根本没有改变。 – Mint 2010-02-19 04:11:05

3

听起来好像您正在使用UTF8字符(')上的标准字符串函数,该字符在ISO 8859-1中不存在。检查您是否使用Unicode compatible PHP设置和功能。另请参阅multibyte字符串函数。

12

您的内容没有问题;问题是报头中的服务器发送:

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: text/plain; charset=utf-8,因为这个页面不是HTML,并使用UTF-8编码。 Mac上的Chromium猜测ISO-8859-1并显示您描述的字符。

如果您不在网站的控制范围内,请将编码指定为UTF-8,无论您使用哪种功能来检索内容。我不熟悉PHP知道如何。

21

确保你的HTML头指定UTF8

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

这通常是卓有成效的,我(很明显,如果内容是UTF8)。

如果您设置了内容类型,则不需要转换为html实体。

+0

这已成为有史以来最伟大的职位!我将我的字符集更新为utf-8,它立即修复了我所有的数据库驱动页面。感谢您的迅速修复! – jlg 2013-04-03 19:30:17

5

如果你在这里,因为你经历你的WordPress网站与垃圾字符的问题,试试这个:

  1. 打开wp-config.php

  2. 注释掉define('DB_CHARSET', 'utf8')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', ''); 
    
0

使用此

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

,而不是这个

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

我所知道的问题得到回答,但设置meta标签在我的情况没有帮助和选择的答案是不够清楚,所以我想提供更简单的答案。

因此,要保持它的简单,存储串入,像这样

$TVrageGiberish = "It’s Getting the Best of Me"; 

$notGiberish = mb_convert_encoding($TVrageGiberish, "HTML-ENTITIES", 'UTF-8'); 

echo $notGiberish; 

应返回你想要的东西It’s Getting the Best of Me

如果你正在分析的东西,你可以在指定执行转换的变量和过程值转换为像这样的变量,其中$TVrage是包含所有值的XML数组,其中$TVrage包含所有值的XML数组,其中标记为“Title”的Feed可能包含特殊字符,如‘’

$cleanedTitle = mb_convert_encoding($TVrage->title, "HTML-ENTITIES", 'UTF-8'); 
1

试试这个:

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

我们有成功利用此去另一个方向:

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

刚刚尝试这一点

如果$text包含奇怪charaters做到这一点:

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

和你做..

1

如果一切似乎没有工作,这可能是你最好的解决方案。

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

== ==或

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

对于fopenfile_put_contents,这将工作:

str_replace("&rsquo;", "'", htmlspecialchars_decode(mb_convert_encoding($string_to_be_fixed, "HTML-ENTITIES", "UTF-8")));