如何插入
每 115 个字符标记一次

发布于 2024-12-23 15:26:06 字数 346 浏览 3 评论 0原文

如何每 115 个字符插入一个
标签?哪个解决方案更好:strlen 还是正则表达式?

我怎样才能做到这一点?

更好理解的是,第6阶段的评论部分是如何制作的。

http://web.archive.org/web /20071120055812/http://stage6.divx.com/user/DorkmanScott

我需要完全格式化的注释。

How can I insert a <br> tag every 115 characters? Which solution is better: strlen or regex?

How I can accomplish this?

Better to understand, how was made the comment section in stage6.

http://web.archive.org/web/20071120055812/http://stage6.divx.com/user/DorkmanScott

I need exactly the formatted comments.

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

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

发布评论

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

评论(2

自控 2024-12-30 15:26:06

这实在做不到。因为如果您天真地在 HTML 文档中每 115 个字符插入一个
标记,您可能最终会进入另一个标记,这显然不是您想要的。您可以使用 HTML 解析器来解析文档,并且仅在文本存在的位置插入
标记,但是,我认为这可能有点问题,因为您必须考虑到对于内联元素,您需要在其中放置
标记。

我猜你的问题与控制网页的宽度有关,你最好使用 CSS 来控制文档的宽度。

This can't really be done. Because if you just naively insert a <br> tag every 115 characters in the HTML document, you might end up within another tag, which would obviously not be what you wanted. You could use an HTML parser to parse the document, and only insert the <br> tag in places where text exists, but, I think that might be a little problematic, as you would have to account for inline elements, where you would want to put a <br> tag.

I'm guessing your problem has something to do with controlling the width of your web page, and you would probably be better off using CSS to control the width of the document.

哎呦我呸! 2024-12-30 15:26:06

尽管注释表明这不是正确的使用方法(即,如果边界元素设置为正确换行,则不需要插入
元素),但如果您绝对需要在 PHP 中实现某些功能,那么 wordrap() 可能是最安全的函数你:

$wrappedString = wordwrap($string, 115, '<br />');

只有当你处理文本,而不是原始 HTML,否则您可能会遇到 DOM 问题。小心行事。

编辑:如果您的目的是打破长单词以避免它们溢出边界框,您可以考虑在较长的单词中添加软连字符 (­) 实体。

Although the comments suggest that this is not the correct approach to use (i.e. you shouldn't need to be inserting <br /> elements if the bounding element is set to wrap correctly), if you absolutely need to achieve something in PHP then wordrap() is probably the safest function for you:

$wrappedString = wordwrap($string, 115, '<br />');

This should only be used if you're dealing with text, not with raw HTML, otherwise you might run into DOM issues. Tread carefully.

EDIT: if your intention is to break long words to avoid them overflowing your bounding box, you could consider adding soft hyphen (­) entities in the longer words.

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