PHP 中的大文本换行

发布于 2024-12-07 04:03:19 字数 413 浏览 0 评论 0原文

我想在一定数量的字符之后换行,例如在 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 技术交流群。

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

发布评论

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

评论(3

萌逼全场 2024-12-14 04:03:19

使用 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 with explode() and implode().

韵柒 2024-12-14 04:03:19

我认为正则表达式是给你想要的最简单的方法。考虑到这个问题,我非常肯定您要么从未听说过它,要么根本不知道它是如何工作的。

因为它非常复杂,所以我愿意帮助你,直到你让它完全按照你想要的方式工作。

我已经进行了一些测试,这对我有用。

echo preg_replace('/(\.{20}?|.{10,20}?\. )/', '$1<br />', $desc);

当达到 20 个字符时,或者当前面有超过 10 个字符时,在有一个点后跟一个空格时,这行代码会放置中断标记,以避免单字行。

您给出的示例文本来自:

接下来的一段类似于“我们的客户服务部门将帮助您解决可能遇到的任何问题。买方负责退货。我们提供免费的 UPS 或 USPS 地面运输到美国大陆我们在付款后 1 个工作日内发货. 所有其他运费均适用(有关详细信息,请参阅所购买商品的拍卖)。

A Paragraph follows like "Our Customer service dept.<br />
will help you with any issue you might have.<br />
Buyer is responsible for return shipping.<br />
We offer free UPS or USPS ground shipping to the continental U.S.<br />
We ship within 1 business day of payment.<br />
All other shipping rates apply (see auction of item purchased for details).<br />
All orders get a UPS or USPS tracking number.

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.

echo preg_replace('/(\.{20}?|.{10,20}?\. )/', '$1<br />', $desc);

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:

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.

to:

A Paragraph follows like "Our Customer service dept.<br />
will help you with any issue you might have.<br />
Buyer is responsible for return shipping.<br />
We offer free UPS or USPS ground shipping to the continental U.S.<br />
We ship within 1 business day of payment.<br />
All other shipping rates apply (see auction of item purchased for details).<br />
All orders get a UPS or USPS tracking number.

Let me know if this is the way you want it!

下壹個目標 2024-12-14 04:03:19
<?php
   $text = "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.";

   $textTrimmed = substr($text, 20) // The number of chars
   echo $textTrimmed
?>

or you can use a function

function trimTextAfter($text, $numberOfChar){
   echo substr($text,$numberOfChar);
}
<?php
   $text = "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.";

   $textTrimmed = substr($text, 20) // The number of chars
   echo $textTrimmed
?>

or you can use a function

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