PHP MySQL 自定义博客,格式化帖子
我正在用 PHP 创建自己的博客,想知道您对我应该如何格式化我的帖子内容的意见。
目前,我将帖子内容存储为纯文本,必要时调用它,然后用 P 标签包裹每一行。我这样做是为了防止我将来想改变文本格式,这样我就不必从数据库中的帖子中删除所有 P 标签了。
现在我遇到的问题是,如果我想添加额外的格式,例如列表等,这些格式也会用 P 标签包装,这是不正确的。
您将如何做到这一点,您会将文本作为纯文本存储在数据库中,还是添加 HTML 格式并将其存储在数据库中?
我不想在数据库中存储不必要的 HTML,但不确定解决方法吗?
I'm creating my own blog in PHP and want to know your opinions on how I should format my post content.
Currently I store the post content as just plain text, call it when necessary, then wrap each line with P tags. I did this in case I wanted to change the way I formatted my text in the future and it would save me the dilema of having to remove all P tags from the posts in the DB.
Now the problem I have this this method is that if I want to add extra formatting in, e.g. lists etc those would also be wrapped with P tags which is not correct.
How would you do this, would you store text as plain text in the DB, or would you add the HTML formatting and store that in the DB to?
I'd prefer not to store unnessary HTML in the DB, but not sure of a way around it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
WordPress 将 html 存储在数据库中。您说您担心在数据库中存储“不必要的”html。是什么让它变得不必要?我认为恰恰相反。您的帖子中可能有标题或粗体或斜体文本。如果存储为纯文本,如何保存此格式?您如何保存您提到的列表?
Wordpress stores html in the db. You say you are concerned about storing 'unnecessary' html in the db. What makes it unnecessary? I think it is the opposite. You may have headings or bold or italic text in your post. If storing as plain text, how do you save this formatting? How are you saving the lists you mentioned?
我认为将原始用户输入存储在数据库中,并在输出时对其进行格式化,并在需要时缓存结果是更好的做法。这样您就可以轻松更改解析事物的方式,而无需使用正则表达式替换数据库内的任何内容。您还可以将原始输入存储在一列中,将格式化的 HTML 存储在另一列中。
我假设您正在使用 Markdown 或 Textile 语法?
I see it as a better practice to store raw user input in the database, and format it on output, caching the result if it is needed. That way you can change the way you are parsing things easily without having to regex-replace anything inside the database. You can also store the raw input in one column, and the formatted HTML in another one.
I assume that you are formatting your raw text with the Markdown or the Textile syntax?
如果您将 HTML 存储在数据库中,则只需单击几下即可摆脱当前情况:
您可以使用
strip_tags()
删除 HTML 格式,如果发生较大更改,您可以在代码上运行 HTML Tidy 来重新映射标签和类。If you store HTML in your DB, you will be just a few clicks away from your current situation:
you can use
strip_tags()
to remove HTML formating and in case of bigger changes, you can run HTML Tidy on your code to remap tags and classes.我认为最好的方法是将 html 保留在数据库中。如果不使用 html,则解析文本的工作量会很大。
看看其他博客工具是如何完成的。例如,我知道 Joomla 将所有 html 保存在数据库中。我知道 Joomla 不是博客工具 :) 但仍然......
I think the best way would be to keep the html in the db. You would have too much to work with parsing the text if you don't use html.
See how it's done in other blog tools. I know that Joomla, for example, keeps all html in the db. I know Joomla isn't blog tool :) but still...