这个字符 ( ? ) 是什么,如何用 PHP 删除它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7186550/
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
What is this character ( ? ) and how do I remove it with PHP?
提问by T. Brian Jones
It's a capital A with a ^ on top: ?
它是一个大写的 A,上面有一个 ^: ?
It is showing up in strings pulled from webpages. It shows up where there was previously an empty space in the original string on the original site. This is the actual character that is stored in my database. It's also what displays on my website when I echo a string that contains it.
它显示在从网页中提取的字符串中。它显示了原始站点上原始字符串中先前有空格的位置。这是存储在我的数据库中的实际字符。当我回显包含它的字符串时,这也是我网站上显示的内容。
I realize it's a character encoding problem when I originally process the webpage, but I am now stuck with these characters in my database. I have to convert this character when it is displayed, or somewhere else in the php before outputting html that contains it. I cannot reprocess the original documents.
当我最初处理网页时,我意识到这是一个字符编码问题,但我现在在我的数据库中使用这些字符。在输出包含它的 html 之前,我必须在显示该字符时或在 php 中的其他位置转换该字符。我无法重新处理原始文件。
I have tried str_replace() and html_entity_decode() and neither do anything.
我试过 str_replace() 和 html_entity_decode() 都没有做任何事情。
What else should I try?
我还应该尝试什么?
回答by James Anderson
"Latin 1" is your problem here. There are approx 65256 UTF-8 characters available to a web page which you cannot store in a Latin-1 code page.
“拉丁语 1”是你的问题。大约有 65256 个 UTF-8 字符可用于网页,您无法将其存储在 Latin-1 代码页中。
For your immediate problem you should be able to
对于您当前的问题,您应该能够
$clean = str_replace(chr(194)," ",$dirty)
However I would switch your database to use utf-8 ASAP as the problem will almost certainly reoccur.
但是,我会尽快将您的数据库切换为使用 utf-8,因为这个问题几乎肯定会再次发生。
回答by Sheepy
It isn't really one character, and is likely caused by misalignment between content encoding and browser's encoding. Try to set the encoding of your outputted page to what you are using.
它实际上不是一个字符,很可能是由内容编码和浏览器编码之间的错位引起的。尝试将输出页面的编码设置为您正在使用的编码。
e.g. In the section, output:
例如在该部分,输出:
echo "<META http-equiv='Content-Type' content='text/html; charset=UTF-8'>";
(Adjust UTF-8 to whatever you are using)
(将 UTF-8 调整为您使用的任何内容)
回答by Grant
This works for me:
这对我有用:
$string = "Sentence ?¢a????not-critical?¢a??a?¢ and \n sorting ?¢a????not-critical?¢a??a?¢ or this \r and some ?¢a????not-critical?¢a??a?¢ more. ' ! -.";
$output = preg_replace('/[^(\x20-\x7F)\x0A\x0D]*/','', $string);
回答by iautomation
I use this one a lot
我经常使用这个
function cleanStr($value){
$value = str_replace('?', '', $value);
$value = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $value);
return $value;
}
回答by roetnig
This problem occurs when using different charset in your web.
在您的网站中使用不同的字符集时会出现此问题。
To solve this (using utf-8 in the examples):
要解决这个问题(在示例中使用 utf-8):
in the <HEAD>
of your page add charset
:
在<HEAD>
您的页面中添加charset
:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
In any form you submit add accept-charset
:
在您提交的任何形式中添加accept-charset
:
<form name="..." method=".." id=".." accept-charset="utf-8">
If you are using php+MySQLi to process your form, you should make sure the database connection is also supporting your charset. Procedural style:
如果你使用 php+MySQLi 来处理你的表单,你应该确保数据库连接也支持你的字符集。程序风格:
mysqli_set_charset($link, "utf8");
and object oriented style:
和面向对象的风格:
$mysqli->set_charset("utf8")
回答by Grace Simmons
I Actually had to have all of this:
我实际上必须拥有所有这些:
<--!DOCTYPE html-->
<--html lang="en-US"-->
<--head-->
<--meta charset="utf-8"-->
<--meta http-equiv="X-UA-Compatible" content="IE=edge"-->
<--meta name="viewport" content="width=device-width, initial-scale=1"-->
<--meta http-equiv="Content-Type" content="text/html; charset=utf-8/" /-->
回答by TechyFlick
To remove a character from string
从字符串中删除一个字符
mysqli_set_charset($con,"utf8");
mysqli_set_charset($con,"utf8");
$price = "a?1 250.00";
$price = "a?1 250.00";
$price2 = preg_replace('/[^(\x20-\x7F)]*/','', $price);
$price2 = preg_replace('/[^(\x20-\x7F)]*/','', $price);
Result : 250.00
结果:250.00
回答by Randheer Katiyar
This is coming from database so the best option will be remove from database using a SQL query like:
这是来自数据库,因此最好的选择是使用 SQL 查询从数据库中删除,例如:
UPDATE products SET description = REPLACE(description, '?', ' ');
回答by Koushik Samanta
Use Bellow codes
使用波纹管代码
echo "<META http-equiv='Content-Type' content='text/html; charset=UTF-8'>";
echo htmlspecialchars_decode($your_string, ENT_QUOTES);