1

我的 db 列中有这种值,

法官-Fürstová Mila "Ut enim ad minim veniam"

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);
        }
    }
    ...
    ...
    ...

 }

当我尝试在输入字段中打印该值时,

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

我的输入字段中只有Judge-Fürstová Mila

如果我htmlentities用来修复双引号问题,

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

我在我的输入字段中得到这个,

法官-Fürstová Mila "Ut enim ad minim veniam"

那么,我怎样才能一次解决这个特殊字符和双引号问题呢?

4

2 回答 2

3

htmlentities()在 PHP5.4 之前默认使用 ISO-8869-1 编码

尝试为函数调用提供编码参数:

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

但是,没有办法绕过提供第二个参数,但ENT_COMPAT | ENT_HTML401无论如何都是默认值。

于 2012-04-19T17:26:06.557 回答
2

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

于 2012-04-19T17:22:42.833 回答