我的mysql数据库表中的数据有"&"之类的特殊字符.
在使用时我需要将它们转换为&.我使用htmlspecialchars将它们转换为HTML实体.
但很少有条目已经有了它&并且它将它们转换为&我需要它们在没有转换的情况下使用它.
该怎么办?
我认为最好的解决方案是先解码它们。Normal&将保持不变,但&被解码为&.
然后再次编码它们以将&其他特殊字符转换为它们编码的等效字符。代码比解释短。:)
$text = 'Your text with &s from the database';
// Decode and re-encode the special characters.
$text = htmlspecialchars(htmlspecialchars_decode($text));
Run Code Online (Sandbox Code Playgroud)
如果您还有其他实体(例如éfor é),则htmlspecialchars您也可以使用htmlentitiesand代替html_entity_decode。解决方案是相同的,但您可以测试哪一个对您产生最佳结果。
$text = 'Your text with &s from the database';
// Decode and re-encode the special characters and other entities.
$text = htmlentities(html_entity_decode($text));
Run Code Online (Sandbox Code Playgroud)
双方htmlspecialchars并htmlentities支持doubleencode参数,默认情况下是真实的,但可以设置为false。这也应该防止双重编码。听起来那个解决方案更干净,但我没有使用它,我不知道它是否有任何副作用。
| 归档时间: |
|
| 查看次数: |
3323 次 |
| 最近记录: |