得到’代替 PHP 中的撇号(')

发布于 2024-08-22 06:23:06 字数 310 浏览 10 评论 0 原文

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

我得到:

"It’s Getting the Best of Me"

应该是:

"It’s Getting the Best of Me"

我从 此网址。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

遥远的绿洲 2024-08-29 06:23:06

要转换为 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 的文档以了解更多编码选项。

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.

许久 2024-08-29 06:23:06

确保你的 html header 指定 utf8

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

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

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

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.

娇纵 2024-08-29 06:23:06

你的内容很好;问题在于服务器发送的标头:

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

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

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.

人间☆小暴躁 2024-08-29 06:23:06

我知道问题已得到解答,但设置元标记对我的情况没有帮助,并且所选答案不够清晰,所以我想提供更简单的答案。

因此,为了简单起见,将字符串存储到一个变量中并像这样进行处理,

$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

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

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

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');
梦里泪两行 2024-08-29 06:23:06

只要尝试一下

,如果 $text 包含奇怪的字符,请执行以下操作:

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

然后您就完成了..

Just try this

if $text contains strange charaters do this:

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

and you are done..

毅然前行 2024-08-29 06:23:06

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

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

==或==

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

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

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

==or==

<?php
$content="It’s Getting the Best of Me";
$content = str_replace("’", "'", $content);
echo $content;
?>
染火枫林 2024-08-29 06:23:06

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

  1. 打开 wp-config.php

  2. 注释掉 define('DB_CHARSET', 'utf8')define('DB_COLLATE', '')

    /** MySQL 主机名 */
    定义('DB_HOST','localhost');
    
    /** 创建数据库表时使用的数据库字符集。 */
    //定义('DB_CHARSET', 'utf8');
    
    /** 数据库整理类型。如果有疑问,请勿更改此设置。 */
    //定义('DB_COLLATE', '');
    

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', '');
    
宣告ˉ结束 2024-08-29 06:23:06

听起来您正在对 ISO 8859-1。检查您是否使用Unicode 兼容 PHP 设置和函数。另请参阅多字节字符串函数。

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.

鸩远一方 2024-08-29 06:23:06

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

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

We had success going the other direction using this:

mb_convert_encoding($text, "HTML-ENTITIES", "ISO-8859-1");
双马尾 2024-08-29 06:23:06

试试这个:

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

try this :

html_entity_decode(mb_convert_encoding(stripslashes($text), "HTML-ENTITIES", 'UTF-8'))
残月升风 2024-08-29 06:23:06

对于 fopenfile_put_contents,这将起作用:

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

For fopen and file_put_contents, this will work:

str_replace("’", "'", htmlspecialchars_decode(mb_convert_encoding($string_to_be_fixed, "HTML-ENTITIES", "UTF-8")));
北风几吹夏 2024-08-29 06:23:06

您应该检查编码来源,然后尝试转换为正确的编码类型。

就我而言,我读取 csv 文件然后导入到数据库。有些文件显示得很好,有些则不然。我检查编码,发现编码为 ASCII 的文件显示良好,其他编码为 UTF-8 的文件已损坏。所以我使用以下代码来转换编码:

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

转换后,我将内容推送到文件,然后处理导入到数据库,现在它在前端显示得很好

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

吃→可爱长大的 2024-08-29 06:23:06

如果上述解决方案均不起作用:

就我而言,我注意到单引号是不同样式的单引号。而不是“我的数据有一个”。注意到单引号的区别了吗?所以我简单地写了一个 str_replace 来替换它,它解决了问题。可能不是最优雅的解决方案,但它完成了工作。

$string= str_replace("’","'",$string);

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("’","'",$string);
百变从容 2024-08-29 06:23:06

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

因此,您只需要弄清楚如何让 PHP 代码将其处理为 UTF-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!

长途伴 2024-08-29 06:23:06

用这个

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

代替这个

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

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" />
花期渐远 2024-08-29 06:23:06

如果没有任何效果,请尝试此 mb_convert_encoding($elem->textContent, 'UTF-8', 'utf8mb4');

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文