PHP 中的大文本换行
我想在一定数量的字符之后换行,例如在 PHP 中的大约 20 个字符之后,它应该在最近的句号(.)之后换行。
我有这样的文字:
后面有一段类似“我们的客户服务部门将为您提供帮助” 解决您可能遇到的任何问题。买家负责退货 船运。我们提供免费的 UPS 或 USPS 地面运输到大陆 美国 我们在付款后 1 个工作日内发货。所有其他运输 费率适用(有关详细信息,请参阅所购买物品的拍卖)。所有订单 获取 UPS 或 USPS 追踪号码。
这是我到目前为止所尝试过的:
$desc = $listings['Item']['Description'];
echo wordwrap($desc,250,"<br />\n");
I want to break a line after certain number of characters like after some 20 characters it should break line after nearest full stop(.) in PHP.
I have a text like this:
A Paragraph follows like "Our Customer service dept. will help you
with any issue you might have. Buyer is responsible for return
shipping. We offer free UPS or USPS ground shipping to the continental
U.S. We ship within 1 business day of payment. All other shipping
rates apply (see auction of item purchased for details). All orders
get a UPS or USPS tracking number.
This is what I've tried so far:
$desc = $listings['Item']['Description'];
echo wordwrap($desc,250,"<br />\n");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
wordwrap()
将字符串包装在给定的字符数。如果您想按点 (.) 分割,可以使用explode()
和implode()
进行操作。Use
wordwrap()
to wrap your string before a given number of characters. If you want to split by dot (.), you can do something withexplode()
andimplode()
.我认为正则表达式是给你想要的最简单的方法。考虑到这个问题,我非常肯定您要么从未听说过它,要么根本不知道它是如何工作的。
因为它非常复杂,所以我愿意帮助你,直到你让它完全按照你想要的方式工作。
我已经进行了一些测试,这对我有用。
当达到 20 个字符时,或者当前面有超过 10 个字符时,在有一个点后跟一个空格时,这行代码会放置中断标记,以避免单字行。
您给出的示例文本来自:
)
。
I think regex is the easiest way to give you what you want. Considering this question, I'm pretty positive you either never heard of it or don't know how it works, at all.
Because it's pretty complicated, I'm willing to help you out until you manage it to work exactly the way you want.
I have ran some tests and this works for me.
This single line of code puts break tags either when 20 characters is reached or when there's a dot followed by a space if there are more than 10 characters before to avoid single word lines.
The example text you gave would go from:
to:
Let me know if this is the way you want it!