Reputation: 1394
htmlspecialchars won't work in my page to remove the back slashes before escaped apostrophes placed there by pdo objects. I am assuming this is because I am using UTF-8 on my page to display Chinese characters. I am trying to display a paragraph in English and a paragraph in Chinese. This is the code I am using:
$english=htmlspecialchars($row["english"], ENT_QUOTES, 'UTF-8');
For some reason it always displays like this:
Don\'t
There is no removal of the escape character...
Upvotes: 0
Views: 828
Reputation: 24146
'
.htmlspecialchars()
do not strip slashesSo, here is usual example of XY problem (can I show this question to students?)
To understand what is going wrong you need to know next things:
\
to input arguments if magic_quotes are enabled - you need to turn them off (via php.ini or during runtime for php < 5.3. Also turn off magic_quotes_sybase.htmlspecialchars()
but without any attributes, as the default ones are okay.If you have 1 or 2 problem - data in your database may already be corrupt, so you need to re-save them without the incorrectly added \
- for this you can use stripslashes()
, but only one time to re-save data. During ordinary work of your system, you need to use proper approach.
Upvotes: 4
Reputation: 449415
Don\'t
This shouldn't be happening in the first place. There is likely a bug further up the stream (like, when the data gets inserted into the database) that should be fixed, instead of treating the symptom.
Upvotes: 2
Reputation: 2564
replace your variable with this:
$english = stripslashes(htmlspecialchars($row["english"], ENT_QUOTES, 'UTF-8'));
the stripslashes part will get rid of the slashes, but it might un-sanitize your string.
Upvotes: 0