deecee2000 Posted November 16, 2009 Share Posted November 16, 2009 Hi All, I was looking some help to wrap my current text. $text = "This is First Test." I want to wrap this above text and expect below output. If I wrap by 3 characters then, Expected Output: Thi- s is Fir- st Tes- t. So let say if the word's character length is more than 3 characters than it will add "-" (Hyphen) and split the remaining characters in to next line. Any help will be appreciated. Thanks, Quote Link to comment https://forums.phpfreaks.com/topic/181754-solved-wordwrap-text/ Share on other sites More sharing options...
rajivgonsalves Posted November 16, 2009 Share Posted November 16, 2009 try http://php.net/manual/en/function.wordwrap.php Quote Link to comment https://forums.phpfreaks.com/topic/181754-solved-wordwrap-text/#findComment-958601 Share on other sites More sharing options...
deecee2000 Posted November 16, 2009 Author Share Posted November 16, 2009 Hey Rajiv, Thnx for quick response. I tried... $text = "This is First Test."; $newtext = wordwrap($text, 3, "-<br />\n", true); And the output is.. Thi- s- is- Fir- st- Tes- t.- So it not seems like expected output. Thanks, Quote Link to comment https://forums.phpfreaks.com/topic/181754-solved-wordwrap-text/#findComment-958608 Share on other sites More sharing options...
Daniel0 Posted November 16, 2009 Share Posted November 16, 2009 I think something like this should do it: function wordwrap2($text, $maxLength, $separator = '-') { $lines = explode("\n", $text); $linesNew = array(); foreach ($lines as $line) { $length = strlen($line); if ($length > $maxLength) { $start = substr($line, 0, $maxLength); if (strlen($start) != strlen(rtrim($start))) { $linesNew[] = rtrim($start); } else { $linesNew[] = $start . $separator; } $linesNew[] = wordwrap2(trim(substr($line, $maxLength)), $maxLength, $separator); } else { $linesNew[] = $line; } } return join("\n", $linesNew); } Quote Link to comment https://forums.phpfreaks.com/topic/181754-solved-wordwrap-text/#findComment-958617 Share on other sites More sharing options...
deecee2000 Posted November 16, 2009 Author Share Posted November 16, 2009 Thnx Daniel0. I was looking the same one. Quote Link to comment https://forums.phpfreaks.com/topic/181754-solved-wordwrap-text/#findComment-958634 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.