简体   繁体   中英

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"

I use PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8" to handle all my special characters,

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.

If I use htmlentities to fix the double quotes issue,

<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"

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

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 is the default anyway.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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