开发者

How to make a string HTML compatible using PHP?

Let's say I have this string:

Hello &, how are you? I'm  fine!

Is there a function that will convert this to:开发者_C百科

Hello &, how are you? I'm fine!

Also, why does "'" sometimes show up as: � on sites? What am I missing?


Yes, htmlspecialchars().

The translations performed are:

  • & (ampersand) becomes &
  • " (double quote) becomes " when ENT_NOQUOTES is not set.
  • ' (single quote) becomes ' only when ENT_QUOTES is set.
  • < (less than) becomes &lt;
  • > (greater than) becomes &gt;


To answer your second question, regarding the improper rendering of characters, it has to do with having straight unicode characters in your file. This is usually the result of pasting in from a program that is using unicode characters. When this unicode isn't converted to ASCII or an HTML entity, it won't be rendered properly in the browser as the browser will try to interpret it as something that it is not.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜