开发者

Wrap large text in PHP

开发者 https://www.devze.com 2023-04-07 16:29 出处:网络
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 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 shi开发者_运维问答pping. 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");


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().


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!


<?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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号