得到一个?代替 PHP 中的撇号(')

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2292004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:54:55  来源:igfitidea点击:

Getting a? instead of an apostrophe(') in PHP

phputf-8character-encodingmojibake

提问by Mint

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

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

I'm getting:

我越来越:

"Ita?s Getting the Best of Me"

It should be:

它应该是:

"It's Getting the Best of Me"

I'm getting this data from this url.

我从这个 url获取这些数据

回答by Matthew

To convert to HTML entities:

转换为 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_encodingfor more encoding options.

有关更多编码选项,请参阅mb_convert_encoding文档。

回答by Ben

Make sure your html header specifies utf8

确保您的 html 标头指定 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).

这通常对我有用(显然如果内容是 utf8)。

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

如果设置了 content-type,则不需要转换为 html 实体。

回答by cobbal

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.

Content-Type 应该设置为Content-type: text/plain; charset=utf-8,因为这个页面不是 HTML 并且使用 utf-8 编码。Mac 上的 Chromium 会猜测 ISO-8859-1 并显示您所描述的字符。

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.

如果您无法控制站点,请将编码指定为 UTF-8,以用于检索内容的任何函数。我对 PHP 不够熟悉,不知道具体如何。

回答by Tumharyyaaden

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 = "Ita?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

哪个应该返回你想要的 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 $TVrageis array with all the values, XML in this example from a feed that has tag "Title" which may contain special characters such as a?or a?.

如果您正在解析某些内容,您可以在将值分配给这样的变量时执行转换,其中$TVrage是包含所有值的数组,在此示例中为 XML 来自具有标记“Title”的提要,其中可能包含特殊字符,例如a?a?

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

回答by questCorp

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

如果您是因为在 WordPress 网站中遇到垃圾字符问题而来到这里,请尝试以下操作:

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

回答by pr1001

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 compatiblePHP settings and functions. See also the multibytestring functions.

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

回答by ShapCyber

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

如果一切似乎都不起作用,这可能是您最好的解决方案。

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

==or==

==或==

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

回答by Rehmat

For fopenand file_put_contents, this will work:

对于fopenand file_put_contents,这将起作用:

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

回答by Softmixt

try this :

尝试这个 :

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

回答by anonymous coward

We had success going the other direction using this:

我们使用这个成功地走向了另一个方向:

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