I have the following string saved in a database: "cocotte & kekette
".
When I query it and display in my web page with PHP, the string "cocotte & kekette
" is displayed.
Can someone tell me how to fix this?
Asked
Active
Viewed 6,414 times
2
-
1Please show some code. The character is converted into entities at some point – Pekka Feb 10 '11 at 14:45
-
1`&` is a reserved character in HTML and must **always be converted** to `&`, even in links' query strings. It is reserved for displaying entities like `é` or `é`. If you see it as `&` it probably means it is double-encoded (i.e: `&`). – netcoder Feb 10 '11 at 15:18
-
at least post the code 1.how you store data in db and 2.how you retrieve data from db. – enam Feb 10 '11 at 18:24
2 Answers
6
Try either
html_entity_decode($string);
Or
mb_convert_encoding($string, "HTML-ENTITIES", "UTF-8");

Dai
- 1,510
- 1
- 11
- 12
-
This answer may be dangerous, depending on the circumstances. If what happened was that data was (consistently and correctly) html entity encoded before storing, the solution is not to decode before outputting, it's just to output as-is from the database. – Artefacto Feb 10 '11 at 14:53
1
That means you probably saved "cocotte & kekette" in the database to begin with. If not, you're double encoding your string.
The right path would be to save the data unencoded in the database. That not being possible, see the $double_encode
argument (set it to FALSE
) in htmlspecialchars
.

Artefacto
- 96,375
- 17
- 202
- 225
-
it's stored as cocotte & kekette in the DB, all functions listed here don't work – Mamadou Feb 10 '11 at 15:04