简体   繁体   中英

How can I remove special characters in a PHP string?

I am getting output as

FBI believed he had a ‘doomsday device’ 

instead of

FBI believed he had a ‘doomsday device’ 

when i am using

iconv("UTF-8", "ISO-8859-1//IGNORE", $topic);

output is

FBI believed he had a âdoomsday deviceâ

I am not using any header or charset in my file.

Update

Got why is this happening

when the UTF-8 series of numbers is interpreted as if it were ISO-8859-1 the output is

’

Explaination

0xE28099 breaks down as 0xE2 (â), 0x80 (€) and 0x99 (™). What was one character in UTF-8 (') gets mistakenly displayed as three (’) when misinterpreted as ISO-8859-1.

Still no solution to convert it

Well the output page is being interpreted in Windows-1252 , not ISO-8859-1..

I recommend setting your header charset to utf-8:

In apache config:

AddDefaultCharset utf-8

Php.ini:

default_charset utf-8

Manually in php:

header("Content-Type: text/html; charset=utf-8");

If you cannot do anything of the above because of some weird reasons, you should then convert into Windows-1252 instead:

iconv("UTF-8", "Windows-1252//IGNORE", $topic);

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