如何使用PHP转换HTML中的特殊字符? [英] How to convert special characters in HTML using PHP?
问题描述
我想要将特殊字符(如智能引号)转换为HTML实体,但不转换其他HTML标记,因为我需要HTML标记工作。
例如,将< div> NVHnoise< / div>
转换为< div> NVH& ldquo;噪声&问题< div>
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>
很奇怪,如果我在我的本地环境中记录这个问题,噪声?。我的本地运行LAMP与php56。服务器运行54和55.我升级到56仍然没有运气。我认为任何东西在PHP配置或其他东西。完全相同的代码。
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.
推荐答案
只有一个翻译的智能引用或一些字符, c> str_replace 可能是唯一的方法:
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);
这篇关于如何使用PHP转换HTML中的特殊字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!