Characters being rendered strangely in HTML/PHP

孤街浪徒 提交于 2019-12-13 09:01:52

问题


I'm presenting an RSS feed with this part of a PHP function:

echo "<li><a target='_blank' href='$item_link'>$item_title</a></li>";

Using an example, this output the following in HTML:

<li>
    <a target='_blank' href='http://www.internationalaccountingbulletin.com/news/ey-shinnihon-will-audit-toshibas-corrected-accounts-while-under-investigation-4639900'>
        EY ShinNihon will audit Toshiba’s corrected accounts… while under investigation
    </a>
</li>

The titles have a large amount of discrepancy when it comes to the symbols used.

It outputs this

EY ShinNihon will audit Toshiba’s corrected accounts… while under investigation

as

EY ShinNihon will audit Toshiba’s corrected accounts… while under investigation

with apostrophes and ellipses (among others) being various symbols prefixed by â€.

How can I convert these symbols back to the originals in PHP?


回答1:


Choose you character encoding to match your what you are editing check this site to learn more. http://htmlpurifier.org/docs/enduser-utf8.html




回答2:


I took out the charset meta tag because I understood that it was bad practice for speed/SEO. When putting it back in, the problem is rectified, thank you. However, is there an alternative that is better practice? Setting headers via PHP - is that prefferable or worse?

So your problem was that you were outputting text in some encoding, without informing the browser what encoding you're giving it, and the browser therefore misinterpreting the text in the wrong encoding, leading to garbage characters. You always need to inform clients about what encoding you're sending them text in. The primary method to do that over HTTP is an HTTP Content-Type header. That way the browser is informed about the type of content it receives before it actually receives the content. Which is exactly as it should be.

HTML <meta> tags are only a fallback. You should include them, since they help specify the encoding of the HTML document should it ever be used outside of an HTTP context (e.g. you just open it from your hard disk, no HTTP involved, no HTTP Content-Type header, no way to specify the encoding... other than the HTML <meta> tag). But again, it should only be a fallback. And there's absolutely no issue with SEO or speed; wherever you got that from, it's pure FUD.




回答3:


This will work for you. first just use mb_convert_encoding() function it will wok for you.

 $item_title = addslashes('this is your text');

 $item_title = mb_convert_encoding($item_title, "HTML-ENTITIES", 'UTF-8');


来源:https://stackoverflow.com/questions/31959896/characters-being-rendered-strangely-in-html-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!