Store html entities in database? Or convert when retrieved?

后端 未结 8 1189
自闭症患者
自闭症患者 2020-12-03 01:13

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

相关标签:
8条回答
  • 2020-12-03 02:05

    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.

    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.

    0 讨论(0)
  • 2020-12-03 02:05

    I have no experience of php but generally I always convert or escape nearest to output. You don't know when your output requirements will change, for example you may want to spit out data as XML, or JSON arrays and so escaping for HTML and then storing means you're limited to using the data as HTML alone.

    0 讨论(0)
提交回复
热议问题