1

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?

Run
  • 54,938
  • 169
  • 450
  • 748

2 Answers2

3

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

German Rumm
  • 5,782
  • 1
  • 25
  • 30
  • Thanks. I just tried and same as the suggestion from Pinetree, I only get Judge-Fürstová Mila instead of Judge-Fürstová Mila "Ut enim ad minim veniam"... – Run Apr 19 '12 at 17:28
  • no sorry, I get this error message actually, `Use of undefined constant ENT_HTML401 - assumed 'ENT_HTML401' in C:\wamp\www\xxx\form_page_update.php on line ...` – Run Apr 19 '12 at 17:33
  • 6
    @lauthiamkok ENT_HTML401 is only available on PHP 5.4+. Just remove the constant from the second parameter and everything should work fine and you'll get the string parsed just right. – Kemal Fadillah Apr 19 '12 at 17:34
  • sorry my mistake, German Rumm. I'm on 5.3.10... it now works with `htmlspecialchars()`. – Run Apr 19 '12 at 17:38
  • 1
    @lauthiamkok, yeah, `htmlspecialchars()` is much better suited for your case. – German Rumm Apr 19 '12 at 17:41
2

Try using htmlspecialchars() instead of htmlentities().

Ivan Pintar
  • 1,861
  • 1
  • 15
  • 27
  • Thanks. I have just tried but I only get `Judge-Fürstová Mila` instead of `Judge-Fürstová Mila "Ut enim ad minim veniam"`... – Run Apr 19 '12 at 17:26
  • `htmlspecialchars()` is actually better than using `htmlentities()` in this case – German Rumm Apr 19 '12 at 17:30
  • @lauthiamkok `htmlspecialchars` should do it, what does the source look like when you use it? – jeroen Apr 19 '12 at 17:31
  • @lauthiamkok Weird, I tried a simple echo in an input [here](http://writecodeonline.com/php/), and it works. Maybe someone else will know if there are some ini settings or what not for htmlspecialchars or htmlentities. – Ivan Pintar Apr 19 '12 at 17:33