2011-09-27 60 views
1

我想在一定数量的字符之后打破一行,比如在大约20个字符之后它应该在PHP中最靠近句号(。)之后断行。用PHP包装大文本

我有这样的文字:。

一个段落如下像“我们的客户服务部将帮助您 与您可能遇到的任何问题买家负责来回 航运我们提供免费的UPS或USPS地面运输到大陆 美国我们船内支付的1个工作日。所有其它运输 收费(参见购买细节项目的拍卖)。所有订单 获得UPS或USPS追踪号码。

这是我到目前为止已经试过:

$desc = $listings['Item']['Description']; 
echo wordwrap($desc,250,"<br />\n"); 
+0

如果你回来选择答案,将不胜感激,哈哈! –

回答

-1
<?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); 
} 
+0

这不会在20个字符后出现的第一个句点之后添加一个换行符。 –

+0

正如你给的长度是20个字符。我想打破20个字符,最近的完整(。)。这意味着“它必须在部门之后打破一条线”)..每隔20个字符最近的fullstop必须换新线 – Viralk

0

使用wordwrap()字符的给定数目之前,包装你的字符串。如果你想通过点(。)分割,你可以用explode()implode()做一些事情。

+0

让我们看看你得到多远,如果你先尝试自己 – Rijk

+0

<?php $ desc = $ listings [ 'Item'] ['Description']; echo wordwrap($ desc,250,“
\ n”);?> – Viralk

0

我认为正则表达式是给你想要的最简单的方法。考虑到这个问题,我相当积极,你要么从来没有听说过它,要么根本不知道它是如何工作的。

因为它非常复杂,我愿意帮助你,直到你完全按照你想要的方式管理它。

我跑了一些测试,这对我有用。

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

这种单一的代码行提出分手标签要么当达到20个字符时,有一个点后面加一个空格,如果有超过10个字符之前,以避免单个字线。

你给将从去的示例文本:。

一个段落如下像“我们的客户服务部将帮助您解决任何问题,你可能有买家负责来回运费我们提供免费的UPS或USPS地面运输到美国大陆我们船内支付的1个工作日内所有其他货运收费(参见购买细节项目的拍卖)所有订单获得UPS或USPS追踪号码

到。:

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. 

让我知道如果这是你想要的方式!