我尝试将文本转换为 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.
发布评论
评论(16)
要转换为 HTML 实体:
请参阅 mb_convert_encoding 的文档以了解更多编码选项。
To convert to HTML entities:
See docs for mb_convert_encoding for more encoding options.
确保你的 html header 指定 utf8
这通常对我有用(显然如果内容是 utf8)。
如果设置了 content-type,则不需要转换为 html 实体。
Make sure your html header specifies utf8
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.
你的内容很好;问题在于服务器发送的标头:
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:
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.
我知道问题已得到解答,但设置元标记对我的情况没有帮助,并且所选答案不够清晰,所以我想提供更简单的答案。
因此,为了简单起见,将字符串存储到一个变量中并像这样进行处理,
这应该返回您想要的内容
It's Getting the Best of Me
如果您正在解析某些内容,则可以在将值分配给 a 时执行转换像这样的变量,其中
$TVrage
是包含所有值的数组,在此示例中,XML 来自具有标签“Title”的 feed,该标签可能包含特殊字符,例如’
或-
。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
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’
.只要尝试一下
,如果
$text
包含奇怪的字符,请执行以下操作:然后您就完成了..
Just try this
if
$text
contains strange charaters do this:and you are done..
如果一切似乎都不起作用,这可能是您最好的解决方案。
==或==
if all seems not to work, this could be your best solution.
==or==
如果您来到这里是因为 WordPress 网站中遇到垃圾字符问题,请尝试以下操作:
打开
wp-config.php
注释掉
define('DB_CHARSET', 'utf8')
和define('DB_COLLATE', '')
If you're here because you're experiencing issues with junk characters in your WordPress site, try this:
Open
wp-config.php
Comment out
define('DB_CHARSET', 'utf8')
anddefine('DB_COLLATE', '')
听起来您正在对 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.
我们使用这个成功地走向了另一个方向:
We had success going the other direction using this:
试试这个:
try this :
对于
fopen
和file_put_contents
,这将起作用:For
fopen
andfile_put_contents
, this will work:您应该检查编码来源,然后尝试转换为正确的编码类型。
就我而言,我读取 csv 文件然后导入到数据库。有些文件显示得很好,有些则不然。我检查编码,发现编码为 ASCII 的文件显示良好,其他编码为 UTF-8 的文件已损坏。所以我使用以下代码来转换编码:
转换后,我将内容推送到文件,然后处理导入到数据库,现在它在前端显示得很好
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:
After convert I push the content to file then process import to DB, now it displays well in front-end
如果上述解决方案均不起作用:
就我而言,我注意到单引号是不同样式的单引号。而不是“我的数据有一个”。注意到单引号的区别了吗?所以我简单地写了一个 str_replace 来替换它,它解决了问题。可能不是最优雅的解决方案,但它完成了工作。
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.
我查看了该链接,对我来说它看起来像是 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!
用这个
代替这个
use this
instead of this
如果没有任何效果,请尝试此 mb_convert_encoding($elem->textContent, 'UTF-8', 'utf8mb4');
If nothing works try this mb_convert_encoding($elem->textContent, 'UTF-8', 'utf8mb4');