从数据库中获取一行并替换其中的一些内容
Getting one row from database and replacing some things in it
我需要从我的数据库中获取一行并替换其中的一些内容,但由于某种原因它没有替换。我做错了什么?
来自数据库的行:
‘Shut up,’ ‘shut your mouth,’ and ‘be quiet' all mean to stop talking, but ‘be quiet' is nice. You don't want to be rude.
test.php:
include("connect.php"); //sql connection
include("functionsX.php"); //just some functions
$response = gd1("response/allData/where response like '%all mean to stop talking%'"); //get one row from database
echo $response."<br>";
$response = str_replace("‘", "'", $response);
$response = str_replace("’", "'", $response);
echo $response;
结果:
‘Shut up,’ ‘shut your mouth,’ and ‘be quiet' all mean to stop talking, but ‘be quiet' is nice. You don't want to be rude.
'Shut up,’ 'shut your mouth,’ and 'be quiet' all mean to stop talking, but 'be quiet' is nice. You don't want to be rude.
需要结果:
‘Shut up,’ ‘shut your mouth,’ and ‘be quiet' all mean to stop talking, but ‘be quiet' is nice. You don't want to be rude.
'Shut up,' 'shut your mouth,' and 'be quiet' all mean to stop talking, but 'be quiet' is nice. You don't want to be rude.
这对我有用:
$str = "‘Shut up,’ ‘shut your mouth,’ and ‘be quiet' all mean to stop talking, but ‘be quiet' is nice. You don't want to be rude.";
$replacements = array('‘', '’');
$nstr = str_replace($replacements, "'", $str);
echo $nstr;
请注意 str_replace()
可以接受数组作为第一个参数(在我的例子中,$replacements
)。那么也许是编码问题?什么时候存回数据库?
看来问题不应该是询问如何替换数据库输出中的奇数字符,而是如何首先防止它们出现。
通常,当您的输出中出现奇数字符时,这意味着您没有在数据库中设置正确的数据库字符集和排序规则。确保您的数据库、table 和列使用像 utf8_general_ci
或 utf8_unicode_ci
这样的字符集。我倾向于使用最后一个。
要了解两种排序规则之间的区别,请阅读这个 SO 问题:What's the difference between utf8_general_ci and utf8_unicode_ci。
您可以更改数据库 and/or table 的字符集和排序规则,如下所示:
ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
或者您可以在 PHPMyAdmin 中执行相同的操作,如下图所示:
您还应该告诉 MySQLi 使用 UTF-8 连接到数据库,如下所示:
$mysqli->set_charset("utf8");
您还可以让 PHP 页面像这样输出 UTF-8:
header('Content-Type: text/html; charset=utf-8');
当你保持字符集像这样对齐时,像 ’
这样的奇数字符应该不会有任何问题,然后你可以 运行 这样:
html_entity_decode($str);
这会将 HTML 个实体(例如 ‘
转换为它们的正确含义,在本例中为 '
.
我需要从我的数据库中获取一行并替换其中的一些内容,但由于某种原因它没有替换。我做错了什么?
来自数据库的行:
‘Shut up,’ ‘shut your mouth,’ and ‘be quiet' all mean to stop talking, but ‘be quiet' is nice. You don't want to be rude.
test.php:
include("connect.php"); //sql connection
include("functionsX.php"); //just some functions
$response = gd1("response/allData/where response like '%all mean to stop talking%'"); //get one row from database
echo $response."<br>";
$response = str_replace("‘", "'", $response);
$response = str_replace("’", "'", $response);
echo $response;
结果:
‘Shut up,’ ‘shut your mouth,’ and ‘be quiet' all mean to stop talking, but ‘be quiet' is nice. You don't want to be rude.
'Shut up,’ 'shut your mouth,’ and 'be quiet' all mean to stop talking, but 'be quiet' is nice. You don't want to be rude.
需要结果:
‘Shut up,’ ‘shut your mouth,’ and ‘be quiet' all mean to stop talking, but ‘be quiet' is nice. You don't want to be rude.
'Shut up,' 'shut your mouth,' and 'be quiet' all mean to stop talking, but 'be quiet' is nice. You don't want to be rude.
这对我有用:
$str = "‘Shut up,’ ‘shut your mouth,’ and ‘be quiet' all mean to stop talking, but ‘be quiet' is nice. You don't want to be rude.";
$replacements = array('‘', '’');
$nstr = str_replace($replacements, "'", $str);
echo $nstr;
请注意 str_replace()
可以接受数组作为第一个参数(在我的例子中,$replacements
)。那么也许是编码问题?什么时候存回数据库?
看来问题不应该是询问如何替换数据库输出中的奇数字符,而是如何首先防止它们出现。
通常,当您的输出中出现奇数字符时,这意味着您没有在数据库中设置正确的数据库字符集和排序规则。确保您的数据库、table 和列使用像 utf8_general_ci
或 utf8_unicode_ci
这样的字符集。我倾向于使用最后一个。
要了解两种排序规则之间的区别,请阅读这个 SO 问题:What's the difference between utf8_general_ci and utf8_unicode_ci。
您可以更改数据库 and/or table 的字符集和排序规则,如下所示:
ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
或者您可以在 PHPMyAdmin 中执行相同的操作,如下图所示:
您还应该告诉 MySQLi 使用 UTF-8 连接到数据库,如下所示:
$mysqli->set_charset("utf8");
您还可以让 PHP 页面像这样输出 UTF-8:
header('Content-Type: text/html; charset=utf-8');
当你保持字符集像这样对齐时,像 ’
这样的奇数字符应该不会有任何问题,然后你可以 运行 这样:
html_entity_decode($str);
这会将 HTML 个实体(例如 ‘
转换为它们的正确含义,在本例中为 '
.