将html实体存储在数据库中吗?还是在检索时转换? [英] Store html entities in database? Or convert when retrieved?

查看:59
本文介绍了将html实体存储在数据库中吗?还是在检索时转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的问题,在将数据插入数据库之前或之后调用htmlentities()(或htmlspecialchars())是一个更好的主意吗?

Quick question, is it a better idea to call htmlentities() (or htmlspecialchars()) before or after inserting data into the database?

之前:新的更长字符串将使我不得不更改数据库以在字段中保存更长的值. (maxlength="800"可能会更改为804个字符的字符串)

Before: The new longer string will cause me to have to change the database to hold longer values in the field. (maxlength="800" could change to a 804 char string)

之后::这将需要更多的服务器处理,并且在每次加载页面或加载AJAX时,可能会进行数百次对htmlspecialchars()的调用.

After: This will require a lot more server processing, and hundreds of calls to htmlspecialchars() could be made on every page load or AJAX load.

太棒了.检索结果时进行转换会大大降低我的代码速度吗?我应该更换数据库吗?

SOOO. Will converting when results are retrieved slow my code significantly? Should I change the DB?

推荐答案

我建议将最原始的数据形式存储在数据库中.在选择如何以及在何处输出数据时,这为您提供了最大的灵活性.

I'd recommend storing the most raw form of the data in the database. That gives you the most flexibility when choosing how and where to output that data.

如果发现性能存在问题,则可以某种方式缓存此数据的HTML格式版本.请记住,过早的优化是一件坏事.

If you find that performance is a problem, you could cache the HTML-formatted version of this data somehow. Remember that premature optimization is a bad thing.

这篇关于将html实体存储在数据库中吗?还是在检索时转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆