I have a string which contains special quote characters (“”), among other characters which might need to be escaped in order to render correctly.
I tried using
htmlentities($string, ENT_QUOTES, "UTF-8")
However it doesn't seem to work on “
and ”
. I should think that they would get turned into “
and ”
, but they don't.
When they are not escaped in my html document, these characters render as in firefox.
Perhaps I have something wrong with my encoding settings, as they seem to be working fine in this post without being escaped..
Yet everything looks correct to me:
The Content-Type
is set to UTF-8 in <head>
:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
According to file
, the document is saved as UTF-8
:
index.phtml: HTML document, UTF-8 Unicode text, with very long lines
Firefox recognizes the file as UTF-8
.
However, straight “ and ”s don't render correctly, yet escaped versions ( “
and ”
) do.
This function converts “
and ”
to "
.
function convert_smart_quotes($string)
{
$search = array(chr(147), chr(148));
$replace = array('"', '"');
return str_replace($search, $replace, $string);
}
So after passing the string which contains “
or ”
to this function it can be passed to
htmlentities($string, ENT_QUOTES, "UTF-8")
as in your case "
render correctly in the browser, the string will be shown correctly.
Another method is to replace the string with “
and ”
$quoteLeft = ' “ ' ;
$quoteRight = ' ” ' ;
$utf = ''.$quoteLeft.'UTF-8'.$quoteRight.'';
htmlentities($string, ENT_QUOTES, $utf);
为了避免使用引号,请使用backslah \\
因此,您的代码基本上看起来像htmlentities($string, ENT_QUOTES, \\"UTF-8\\")
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.