问题
Im looking to convert special characters like smart quotes to HTML entities but without converting other HTML markup because I need HTML markup to work.
For example, convert <div>NVH “noise”</div>
to <div>NVH “noise” issues<div>
Its strange that if I log this on my local environment I get “noise” with smartquotes but on server I got ?noise?. My local runs LAMP with php56. server ran 54 and 55. I upgraded to 56 still no luck. I think either something in php configuration or among other things. Same exact code.
回答1:
For only one translation of the smart quotes, or for some characters and not others, str_replace
is probably the only way to go:
$string = str_replace(array('“','”'), array('“','”'), $string);
回答2:
Try php htmlspecialchars method
<?php
$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; // <a href='test'>Test</a>
?>
来源:https://stackoverflow.com/questions/28885466/how-to-convert-special-characters-in-html-using-php