简体   繁体   English

PHP中的特殊字符和双引号问题

[英]Special characters and double quotes issue in PHP

I have this kind of value in my db column, 我的数据库列中有这种值,

Judge-Fürstová Mila "Ut enim ad minim veniam" 菲尔斯托娃·米拉(FürstováMila)法官

I use PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8" to handle all my special characters, 我使用PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"处理我所有的特殊字符,

class database_pdo
{
    # database handler
    protected $connection = null;

    # make a connection
    public function __construct($dsn,$username,$password)
    {
        try 
        {

            $this->connection = new PDO($dsn, $username, $password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
            $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

        }
        catch (PDOException $e) 
        {
            # call the get_error function
            $this->get_error($e);
        }
    }
    ...
    ...
    ...

 }

And when I try to print that value in my input field, 当我尝试在输入字段中打印该值时,

<input name="title" type="text" value="<?php echo $page->title;?>"/>

I only get Judge-Fürstová Mila in my input field. 我在输入字段中仅得到FürstováMila法官

If I use htmlentities to fix the double quotes issue, 如果我使用htmlentities修复双引号问题,

<input name="title" type="text" value="<?php echo htmlentities($page->title);?>"/>

I get this in my input field, 我在输入字段中得到这个

Judge-Fürstová Mila "Ut enim ad minim veniam" 法官弗尔斯托瓦·米拉(Us enim ad minim veniam)

So, how can I fix this special characters and double quotes issue at once? 那么,如何立即解决此特殊字符和双引号问题?

htmlentities() works with ISO-8869-1 encoding by default prior to PHP5.4 htmlentities()在PHP5.4之前默认使用ISO-8869-1编码

Try supplying encoding parameter to a function call: 尝试为函数调用提供编码参数:

<?php echo htmlentities($page->title, ENT_COMPAT | ENT_HTML401, 'UTF-8');?>

No way to bypass supplying second parameter, though, but ENT_COMPAT | ENT_HTML401 尽管没有办法绕过提供第二个参数,但是ENT_COMPAT | ENT_HTML401 ENT_COMPAT | ENT_HTML401 is the default anyway. ENT_COMPAT | ENT_HTML401是默认设置。

尝试使用htmlspecialchars()而不是htmlentities()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM