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

时间:2022-09-15 15:16:31

This is really weird to explain, so here it goes...

解释真的很奇怪,所以这里......

I'm fetching data from phpmyadmin that contains unescaped single quotes. I'm trying to convert ' to ' by using Content-Type: text/html; in a php header.

我从phpmyadmin获取包含未转义单引号的数据。我正在尝试将'转换为'使用Content-Type:text / html;在一个PHP标题。

In one column of the database, this is the value: This year's...

在数据库的一列中,这是价值:今年的......

I set the content type to <?php header('Content-Type: text/html; charset=UTF-8'); ?>

我将内容类型设置为

I fetched the data with mysql_fetch_assoc(), so it should output, This year&apos;s. But, it outputs This year�s....

我用mysql_fetch_assoc()获取数据,因此它应该输出,今年的。但是,它输出了今年的......

I then manually deleted This year's... from the database and replaced it with the same This year's... (directly in the database). I set the content type to text/html again and got this:

然后我手动删除今年的...从数据库中取而代之的是今年的...(直接在数据库中)。我再次将内容类型设置为text / html并获得:

This year&apos;s...

That's it... this is almost imossible to explain. A single quote is submitted to the database through a form. I fetch it and set the content type to text/html and it outputs ' as . If I go into the database, manually delete the ', and replace it with the same ', it then outputs &apos;.

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

Has anyone ever experienced this weird quirk or recognize what it might be?

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

UPDATE:

The value in the database is This year's which was uploaded by a form. The quote is unescaped in the database. Here's how I'm fetching the data. I want the ' to be converted to &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(); ?>

It outputs This year�s.

它输出今年 s。

I then delete This year's from the database and manually write This year's in it's place. It outputs this This year&apos;s.

然后我从数据库中删除今年,并手动编写今年的内容。它输出了今年的情况。

1 个解决方案

#1


0  

before inserting in to database Convert special characters to HTML entities

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

htmlspecialchars("This year's");

#1


0  

before inserting in to database Convert special characters to HTML entities

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

htmlspecialchars("This year's");