I'm trying to display the pound symbol in HTML (from PHP) but all I get is a symbol with a question mark.
我试图在HTML(来自PHP)中显示#符号,但我得到的只是一个带有问号的符号。
The following are things that I've tried.
以下是我尝试过的。
In PHP:
在PHP中:
header('Content-type: text/html; charset=utf-8');
In HTML, put this in the head tag:
在HTML中,把这个放在head标签中:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
I tried displaying it using a javascript function which converts it to:
我尝试使用一个javascript函数来显示它,将它转换为:
&#65533;
I suppose it would help if I knew what I was doing... but I guess that's why I'm asking this question :)
我想如果我知道我在做什么……但我想这就是我问这个问题的原因
8 个解决方案
#1
16
Educated guess: You have a ISO-8859-1 encoded pound sign in a UTF-8 encoded page.
有经验的猜测:在UTF-8编码页中有一个ISO-8859-1编码的磅符号。
Make sure your data is in the right encoding and everything will work fine.
确保您的数据是正确的编码,一切都会正常工作。
#2
16
Use £
. I had the same problem and solved it using jQuery:
使用& # 163;。我遇到了同样的问题,用jQuery解决了:
$(this).text('&#163;');
- If you try this and it does not work, just change the jQuery methods,
- 如果你尝试了这个,但是没有成功,只要改变jQuery方法,
$(this).html('&#163;');
- This always work in all contexts...
- 这在任何情况下都适用……
#3
11
1st: the pound symbol is a "special" char in utf8 encoding (try saving £$ in a iso-8859-1 (or iso-8859-15) file and you will get ä when encoding using header)
1:英镑符号是一种“特殊”char utf8编码(尝试拯救美元£iso - 8859 - 1(或iso - 8859 - 15)文件时,你会得到一个编码使用标题)
2nd: change your encoding to utf8 form the file. there are plenty of methods to do it. notepad and notepad++ are great sugestions.
第二:将编码从文件中更改为utf8。有很多方法可以做到这一点。记事本和记事本++是很棒的糖。
3rd: use ob_start(); (in php) BEFORE YOU MAKE ANY OUTPUT if you are getting weird encoding errors, like missing the encoding sometimes. and YES, this solves it! this kind of errors occurs when a page is encoded in windows-1252(ANSI),ASCII,iso-8859-1(5) and then you have all the others in utf8. this is a terrible error and can cause weird things like session_start(); not working.
3:使用ob_start();(在php中),如果您的编码出现了奇怪的编码错误,那么在您生成任何输出之前,比如有时会丢失编码。是的,这解决了问题!当一个页面被用windows-1252(ANSI)、ASCII (ASCII)、iso-8859-1(5)编码时,就会出现这种错误。这是一个可怕的错误,会导致一些奇怪的事情,比如session_start();不工作。
4th: other php solutions:
4:其他php解决方案:
utf8_encode('£');
htmlentities('£');
echo '&pound;';
5th: javascript solutions:
第五:javascript解决方案:
document.getElementById('id_goes_here').innerText.replace('£','&pound;');
document.getElementById('id_goes_here').innerText.replace('£',"\u00A3");
$(this).html().replace('£','&pound;'); //jquery
$(this).html().replace('£',"\u00A3"); //jquery
String.fromCharCode('163');
you MUST send £, so it will repair the broken encoded code point. please, avoid these solutions! use php! these solutions only show how to 'fix' the error, and the last one only to create the well-encoded char.
你必须发送£,所以它将修复破碎的编码代码点。请避免这些解决方案!使用php !这些解决方案只显示如何“修复”错误,最后一个解决方案只创建编码良好的char。
#5
3
You could try using £
or £
instead of embedding the character directly; if you embed it directly, you're more likely to run into encoding issues in which your editor saves the file is ISO-8859-1 but it's interpreted as UTF-8, or vice versa.
你可以尝试使用&英镑;或& # 163;而不是直接嵌入字符;如果您直接嵌入它,您更可能遇到编码问题,其中编辑器保存的文件是ISO-8859-1,但它被解释为UTF-8,反之亦然。
If you want to embed it (or other Unicode characters) directly, make sure you actually save your file as UTF-8, and set the encoding as you did with the Content-Type
header. Make sure when you get the file from the server that the header is present and correct, and that the file hasn't been transcoded by the web server.
如果您想要直接嵌入它(或其他Unicode字符),请确保您实际上将文件保存为UTF-8,并像使用Content-Type header那样设置编码。请确保当您从服务器获得文件时,头是存在的并且是正确的,并且该文件没有被web服务器转换。
#6
2
Or for other code equivalents try:
或者对于其他代码等价物,请尝试:
£
£
#7
1
You need to save your PHP script file in UTF-8 encoding, and leave the <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
in the HTML.
您需要将PHP脚本文件保存为UTF-8编码,并保留 。
For text editor, I recommend Notepad++, because it can detect and display the actual encoding of the file (in the lower right corner of the editor), and you can convert it as well.
对于文本编辑器,我推荐记事本++,因为它可以检测和显示文件的实际编码(在编辑器的右下角),您也可以对它进行转换。
#8
1
This works in all chrome, IE, Firefox.
这适用于所有的chrome, IE, Firefox。
In Database > table > field type .for example set the symbol column TO varchar(2) utf8_bin
php code:
在数据库>表>字段类型中,例如将符号列设置为varchar(2) utf8_bin php代码:
$symbol = '£';
echo mb_convert_encoding($symbol, 'UTF-8', 'HTML-ENTITIES');
or
html_entity_decode($symbol, ENT_NOQUOTES, 'UTF-8');
And also make sure set the HTML OR XML encoding to encoding="UTF-8"
还要确保将HTML或XML编码设置为编码="UTF-8"
Note: You should make sure that database, document type and php code all have a same encoding
注意:您应该确保数据库、文档类型和php代码都具有相同的编码
How ever the better solution would be using £
更好的解决方案是使用&英镑;
#1
16
Educated guess: You have a ISO-8859-1 encoded pound sign in a UTF-8 encoded page.
有经验的猜测:在UTF-8编码页中有一个ISO-8859-1编码的磅符号。
Make sure your data is in the right encoding and everything will work fine.
确保您的数据是正确的编码,一切都会正常工作。
#2
16
Use £
. I had the same problem and solved it using jQuery:
使用& # 163;。我遇到了同样的问题,用jQuery解决了:
$(this).text('&#163;');
- If you try this and it does not work, just change the jQuery methods,
- 如果你尝试了这个,但是没有成功,只要改变jQuery方法,
$(this).html('&#163;');
- This always work in all contexts...
- 这在任何情况下都适用……
#3
11
1st: the pound symbol is a "special" char in utf8 encoding (try saving £$ in a iso-8859-1 (or iso-8859-15) file and you will get ä when encoding using header)
1:英镑符号是一种“特殊”char utf8编码(尝试拯救美元£iso - 8859 - 1(或iso - 8859 - 15)文件时,你会得到一个编码使用标题)
2nd: change your encoding to utf8 form the file. there are plenty of methods to do it. notepad and notepad++ are great sugestions.
第二:将编码从文件中更改为utf8。有很多方法可以做到这一点。记事本和记事本++是很棒的糖。
3rd: use ob_start(); (in php) BEFORE YOU MAKE ANY OUTPUT if you are getting weird encoding errors, like missing the encoding sometimes. and YES, this solves it! this kind of errors occurs when a page is encoded in windows-1252(ANSI),ASCII,iso-8859-1(5) and then you have all the others in utf8. this is a terrible error and can cause weird things like session_start(); not working.
3:使用ob_start();(在php中),如果您的编码出现了奇怪的编码错误,那么在您生成任何输出之前,比如有时会丢失编码。是的,这解决了问题!当一个页面被用windows-1252(ANSI)、ASCII (ASCII)、iso-8859-1(5)编码时,就会出现这种错误。这是一个可怕的错误,会导致一些奇怪的事情,比如session_start();不工作。
4th: other php solutions:
4:其他php解决方案:
utf8_encode('£');
htmlentities('£');
echo '&pound;';
5th: javascript solutions:
第五:javascript解决方案:
document.getElementById('id_goes_here').innerText.replace('£','&pound;');
document.getElementById('id_goes_here').innerText.replace('£',"\u00A3");
$(this).html().replace('£','&pound;'); //jquery
$(this).html().replace('£',"\u00A3"); //jquery
String.fromCharCode('163');
you MUST send £, so it will repair the broken encoded code point. please, avoid these solutions! use php! these solutions only show how to 'fix' the error, and the last one only to create the well-encoded char.
你必须发送£,所以它将修复破碎的编码代码点。请避免这些解决方案!使用php !这些解决方案只显示如何“修复”错误,最后一个解决方案只创建编码良好的char。
#4
#5
3
You could try using £
or £
instead of embedding the character directly; if you embed it directly, you're more likely to run into encoding issues in which your editor saves the file is ISO-8859-1 but it's interpreted as UTF-8, or vice versa.
你可以尝试使用&英镑;或& # 163;而不是直接嵌入字符;如果您直接嵌入它,您更可能遇到编码问题,其中编辑器保存的文件是ISO-8859-1,但它被解释为UTF-8,反之亦然。
If you want to embed it (or other Unicode characters) directly, make sure you actually save your file as UTF-8, and set the encoding as you did with the Content-Type
header. Make sure when you get the file from the server that the header is present and correct, and that the file hasn't been transcoded by the web server.
如果您想要直接嵌入它(或其他Unicode字符),请确保您实际上将文件保存为UTF-8,并像使用Content-Type header那样设置编码。请确保当您从服务器获得文件时,头是存在的并且是正确的,并且该文件没有被web服务器转换。
#6
2
Or for other code equivalents try:
或者对于其他代码等价物,请尝试:
£
£
#7
1
You need to save your PHP script file in UTF-8 encoding, and leave the <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
in the HTML.
您需要将PHP脚本文件保存为UTF-8编码,并保留 。
For text editor, I recommend Notepad++, because it can detect and display the actual encoding of the file (in the lower right corner of the editor), and you can convert it as well.
对于文本编辑器,我推荐记事本++,因为它可以检测和显示文件的实际编码(在编辑器的右下角),您也可以对它进行转换。
#8
1
This works in all chrome, IE, Firefox.
这适用于所有的chrome, IE, Firefox。
In Database > table > field type .for example set the symbol column TO varchar(2) utf8_bin
php code:
在数据库>表>字段类型中,例如将符号列设置为varchar(2) utf8_bin php代码:
$symbol = '£';
echo mb_convert_encoding($symbol, 'UTF-8', 'HTML-ENTITIES');
or
html_entity_decode($symbol, ENT_NOQUOTES, 'UTF-8');
And also make sure set the HTML OR XML encoding to encoding="UTF-8"
还要确保将HTML或XML编码设置为编码="UTF-8"
Note: You should make sure that database, document type and php code all have a same encoding
注意:您应该确保数据库、文档类型和php代码都具有相同的编码
How ever the better solution would be using £
更好的解决方案是使用&英镑;