如何<br>
每 115 个字符插入一个标签?哪种解决方案更好:strlen 或 regex?
我怎么能做到这一点?
更好地理解,如何在 stage6 中制作评论部分。
http://web.archive.org/web/20071120055812/http://stage6.divx.com/user/DorkmanScott
我需要完全格式化的评论。
如何<br>
每 115 个字符插入一个标签?哪种解决方案更好:strlen 或 regex?
我怎么能做到这一点?
更好地理解,如何在 stage6 中制作评论部分。
http://web.archive.org/web/20071120055812/http://stage6.divx.com/user/DorkmanScott
我需要完全格式化的评论。
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.
尽管评论表明这不是正确的使用方法(即,<br />
如果边界元素设置为正确换行,则不需要插入元素),但如果您绝对需要在 PHP 中实现某些目标,那么wordrap()可能是最安全的功能:
$wrappedString = wordwrap($string, 115, '<br />');
只有在处理文本而不是原始 HTML 时才应该使用它,否则可能会遇到 DOM 问题。小心行事。
编辑:如果您的意图是打破长词以避免它们溢出您的边界框,您可以考虑­
在较长的词中添加软连字符 ( ) 实体。