php 字符限制(修剪 html 段落)

发布于 2025-01-03 23:46:43 字数 390 浏览 0 评论 0原文

我们有自己的博客系统,帖子数据存储原始 html,因此当从数据库调用它时,我们可以直接回显它,并且它已完全格式化,在我们的情况下不需要 BB 代码。我们现在的问题是我们的博客文章有时太长,需要修剪。

问题是我们的数据包含html,主要是

< ;b> 和其他样式标签。我制作了一个 php 函数来修剪字符,但它没有考虑 html 标签。如果修剪功能修剪博客,它不应该修剪标签,因为它会弄乱整个页面。该函数需要能够关闭 html 标签(如果它们被修剪)。有没有一个函数可以做到这一点?或者我可以从中启动和构建的函数?

We have our own blog system and the post data is stores a raw html, so when it's called from the db we can just echo it and it's formatted completely, no need for BB codes in our situation. Our issue now is that our blog posts sometimes are too long and need to be trimmed.

The problem is that our data contains html, mostly <font>, <span>, <p>, <b>, and other styling tags. I made a php function that trims the characters, but it doesn't take into account the html tags. If the trim function trims the blog it should not trim tags because it messes the whole page. The function needs to be able to close the html tags if they're trimmed. Is there a function out there that can do this? or a function where I could start and build from it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤城病女 2025-01-10 23:46:43

这里有一个在保留 HTML 标记的同时截断文本的好例子。

There's a good example here of truncating text while preserving HTML tags.

↘人皮目录ツ 2025-01-10 23:46:43

strip_tags 可以删除所有 HTML 标签,但除此之外就没有太多了。

顺便说一句,这并不是一件容易的事情,您必须实际解析 HTML 以找出哪些标签保持打开状态 - 无论如何,这是最可靠的方法。另外,不要使用正则表达式< /a>.

There is strip_tags which gets rid of all HTML tags but other than that there isn't much.

This is not an easy thing by the way, you have to actually parse the HTML to find out which tags are left open - that's the most robust approach anyway. Also, don't use a regular expression.

贪了杯 2025-01-10 23:46:43

正确的解决方案是不在数据库层中存储显示信息。

如果做不到这一点,您可以使用 CSS overflow 属性:打印整个帖子,然后让显示层处理调整其大小以适应。通过将调整大小(显示问题,而不是内容问题)也放入显示层中,这可以缓解数据库中格式化信息的问题。

如果失败,您可以解析 HTML 并“向上舍入”或“向下舍入”到最近的标记边界,然后插入完成您所在块所需的标记结束字符。

另一个选项是iframe 内容。

The right solution is to not store display information in your database layer.

Failing that, you could use CSS overflow properties: print the whole post, and then have the display layer handle sizing it to fit. This mitigates the problem of having formatting information in your database by putting the resizing (a display issue, not a content issue) into the display layer as well.

Failing that, you could parse the HTML and "round up" or "round down" to the nearest tag boundary, then insert the tag-close characters necessary to finish the block you were in.

Another option is to iframe the content.

笑叹一世浮沉 2025-01-10 23:46:43

我知道这不是以编程方式执行此操作的最佳方法,但是您是否考虑过手动指定剪切位置?添加诸如手动剪切之类的内容将允许您控制剪切发生的位置,而不管其前面有多少字符。例如,您始终可以将其放在第一段下方。

诚然,你失去了让它自动发生的能力,但我会提出它,以防这对你来说不太重要。

I know this isn't the best way to do it programatically, but have you considered manually specifying where the cut should be? Adding something like and cutting it there manually would allow you to control where the cut happened, regardless of the number of characters before it. For example, you could always put that below the first paragraph.

Admittedly, you lose the ability to just have it happen automatically, but I bring it up in case that doesn't matter as much to you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文