gpt4 book ai didi

PHP 不会 str_replace 数据库中的单引号

转载 作者:行者123 更新时间:2023-11-30 01:36:31 28 4
gpt4 key购买 nike

解释起来确实很奇怪,所以就这样......

我正在从 phpmyadmin 获取包含未转义单引号的数据。我正在尝试转换''通过使用Content-Type: text/html;在 php header 中。

在数据库的一列中,该值是:This year's...

我将内容类型设置为 <?php header('Content-Type: text/html; charset=UTF-8'); ?>

我使用 mysql_fetch_assoc() 获取数据,所以它应该输出,This year&apos;s 。但是,它输出 This year�s... .

然后我手动删除了This year's...从数据库中并将其替换为相同的 This year's... (直接在数据库中)。我将内容类型设置为 text/html再次得到这个:

This year&apos;s...

就是这样...这几乎无法解释。单引号通过表单提交到数据库。我获取它并将内容类型设置为 text/html 并输出 ' 。如果我进入数据库,手动删除 ' ,并将其替换为相同的 ' ,然后输出 &apos; .

有没有人经历过这种奇怪的怪癖或认识到它可能是什么?

更新:

数据库中的值为This year's这是通过表格上传的。该引用在数据库中未转义。这是我获取数据的方式。我想要'转换为&apos; .

<?php
header('Content-Type: text/html; charset=UTF-8');
include('init.inc.php');

function get_feeds() {
$query = mysql_query("SELECT * FROM test ORDER BY id DESC LIMIT 1");
while($fetch = mysql_fetch_assoc($query)) {

$text = $fetch['text'];

echo $text;

}
}

?>
<?php get_feeds(); ?>

它输出This year�s .

然后我删除 This year's从数据库中手动写入 This year's在它的地方。它输出这个 This year&apos;s .

最佳答案

插入数据库之前将特殊字符转换为 HTML 实体

htmlspecialchars("This year's");

关于PHP 不会 str_replace 数据库中的单引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16806034/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com