0

I want to make this character (′) html entity for storing in my database using php. I am using MySQL databse. I use:

$string = "HTML5′s placeholder Attribute";
$newStr = htmlspecialchars($string, ENT_QUOTES);
echo $newStr;

the above code prints the following

HTML5â?²s placeholder Attribute 

How can I make this (′) character an HTML entity?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Rizwan Khan
  • 293
  • 3
  • 9
  • 17
  • 1
    That character's not a special character in HTML, you shouldn't escape it as such. Use your database driver's string escaping functions, or better yet a database API that gives you parameter interpolation. (Not knowing what database you're using and what libraries are available in your PHP install, it's not really possible to make a less vague answer.) – millimoose Jan 14 '12 at 17:53
  • @Inerdial I am using MySQL database – Rizwan Khan Jan 14 '12 at 17:59
  • 1
    Then the simplest solution is using [`mysql_real_escape_string`](http://php.net/manual/en/function.mysql-real-escape-string.php), and using the same encoding (preferrably UTF-8) end to end. – millimoose Jan 14 '12 at 18:08

1 Answers1

4

You need to use htmlentities() here, as that specific quote thingy does not need escaping normally. And you also need to specifiy the charset, as you otherwise get those Latin-1 equivalent escapes:

echo htmlentities("′", ENT_QUOTES, "UTF-8");

Should either get you ′ or ′ as result.

mario
  • 144,265
  • 20
  • 237
  • 291
  • How i can convert this from a string, like this string "HTML5′s placeholder Attribute". – Rizwan Khan Jan 14 '12 at 18:00
  • You use your string as first parameter, like in your original example. – mario Jan 14 '12 at 18:01
  • I use my string as first parameter. it prints nothing. – Rizwan Khan Jan 14 '12 at 18:07
  • No, it's not an output function. Add `echo` before it. – mario Jan 14 '12 at 18:08
  • that's code working fine echo htmlentities("HTML5′s placeholder Attribute", ENT_QUOTES, "UTF-8"); . But when i store my string to db $str = htmlentities("HTML5′s placeholder Attribute", ENT_QUOTES, "UTF-8"); . It stores an empty value – Rizwan Khan Jan 14 '12 at 18:17
  • See what @Inerdial wrote. If this is just an elaborate database charset workaround, then don't do it. Use [`SET NAMES UTF8`](http://stackoverflow.com/questions/2159434/set-names-utf8-in-mysql) and the right table schemes for mysql rather. Also use `mysql_error()` to find the reason for failed querys and inserts. – mario Jan 14 '12 at 18:21
  • I execute this SET NAMES UTF8 to my database. If the string has no (′). The string stores in the database successfully. But If string has (′). The query stores a blank value. Please Help me – Rizwan Khan Jan 14 '12 at 18:40