By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,777 Members | 1,311 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,777 IT Pros & Developers. It's quick & easy.

php word wrap function needed - one that works

P: n/a
I have been looking for a php word wrap function. I know there's an
official PHP function but I've tried that and many of the functions
contributed on that php.net page and none do what I want...and some act
weird as well.

I want, If I say break at 150 chars then the function will break at the
nearest space to 150 ie. it will actually break at the word boundry. And
if theres a url it will not break inside a URL, it will leave URLS alone.

Please state/link me *specifically* what function if you know of one,
Thanks for the help,
Lee G.
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
function iTrunc($string, $length) {
if (strlen($string)<=$length) {
return $string;
}

$pos = strrpos($string,".");
if ($pos>=$length-4) {
$string = substr($string,0,$length-4);
$pos = strrpos($string,".");
}
if ($pos>=$length*0.4) {
return substr($string,0,$pos+1)." ...";
}

$pos = strrpos($string," ");
if ($pos>=$length-4) {
$string = substr($string,0,$length-4);
$pos = strrpos($string," ");
}
if ($pos>=$length*0.4) {
return substr($string,0,$pos)." ...";
}

return substr($string,0,$length-4)." ...";

}
from an unknown author..
but with slight modification you should be able to get what you want..
this just takes the string, finds the nearest period, word end, or
space at the spec. length and returns the string. This could be
helpful, i'm not sure if it solves any problems.

Jul 17 '05 #2

P: n/a
jblanch wrote:
function iTrunc($string, $length) {
if (strlen($string)<=$length) {
return $string;
}

$pos = strrpos($string,".");
if ($pos>=$length-4) {
$string = substr($string,0,$length-4);
$pos = strrpos($string,".");
}
if ($pos>=$length*0.4) {
return substr($string,0,$pos+1)." ...";
}

$pos = strrpos($string," ");
if ($pos>=$length-4) {
$string = substr($string,0,$length-4);
$pos = strrpos($string," ");
}
if ($pos>=$length*0.4) {
return substr($string,0,$pos)." ...";
}

return substr($string,0,$length-4)." ...";

}
from an unknown author..
but with slight modification you should be able to get what you want..
this just takes the string, finds the nearest period, word end, or
space at the spec. length and returns the string. This could be
helpful, i'm not sure if it solves any problems.
I don't know if I can modfiy this to ignore links...

Jul 17 '05 #3

P: n/a
leegold2 wrote:
I don't know if I can modfiy this to ignore links...


One approach would be to let wordwrap do its stuff and replace the unwanted
breaks, example:

function mywordwrap ($str, $length) {
$str = wordwrap($str, $length);
return nl2br(
preg_replace(
"/<([^>]*)[\r\n]([^>]*)>/s",
"<$1 $2>",
$str
)
);
}
JW

Jul 17 '05 #4

P: n/a
Hello,

on 01/02/2005 03:58 AM leegold2 said the following:
I have been looking for a php word wrap function. I know there's an
official PHP function but I've tried that and many of the functions
contributed on that php.net page and none do what I want...and some act
weird as well.

I want, If I say break at 150 chars then the function will break at the
nearest space to 150 ie. it will actually break at the word boundry. And
if theres a url it will not break inside a URL, it will leave URLS alone.

Please state/link me *specifically* what function if you know of one,


Yes, I also had to ditch PHP wordwrap function because of its bugs. I
use my own implementation in a class that I use composing and sending
e-mail message. It is used to break long lines and optionally mark them
with some quoting marks if necessary.

It is just meant to break plain text but you are free to modify it to
skip HTML tags:

http://www.phpclasses.org/mimemessage
--

Regards,
Manuel Lemos

PHP Classes - Free ready to use OOP components written in PHP
http://www.phpclasses.org/

PHP Reviews - Reviews of PHP books and other products
http://www.phpclasses.org/reviews/

Metastorage - Data object relational mapping layer generator
http://www.meta-language.net/metastorage.html
Jul 17 '05 #5

P: n/a
leegold2 wrote:
I don't know if I can modfiy this to ignore links...


One approach would be to let wordwrap do its stuff and replace the unwanted
breaks, example:

function mywordwrap ($str, $length) {
$str = wordwrap($str, $length);
return nl2br(
preg_replace(
"/<([^>]*)[\r\n]([^>]*)>/s",
"<$1 $2>",
$str
)
);
}
JW

Jul 17 '05 #6

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

leegold2 wrote:

| I have been looking for a php word wrap function. I know there's an
| official PHP function but I've tried that and many of the functions
| contributed on that php.net page and none do what I want...and some act
| weird as well.
|
| I want, If I say break at 150 chars then the function will break at the
| nearest space to 150 ie. it will actually break at the word boundry. And
| if theres a url it will not break inside a URL, it will leave URLS alone.
|
| Please state/link me *specifically* what function if you know of one,
| Thanks for the help,
| Lee G.

Here is one that I had thrown together a while back:

function word_wrap($chars,$str){
~ $cpy=strip_tags($str);
~ $chk=array_reverse(preg_split('`\s`',$cpy));
~ $chk2=array_reverse(preg_split('`\s`',$str));
~ $len=0;
~ $retVal='';

~ // we want to work backwards on this
~ for($i=count($chk)-1;$i>=0;$i--){
~ // $len is the current segment length in the stripped string
~ if($len>0 && ($len + strlen($chk[$i])) > $chars){
~ // add a line break
~ $retVal.='<br />'."\n";
~ $len=0;
~ }else if($len>0){
~ // space between words needs to be counted
~ $len++;
~ }

~ // add the necessary pieces to the string
~ $pop1=array_pop($chk); // get next piece from each version
~ $pop2=array_pop($chk2);
~ $retVal.=$pop2.' ';
~ $len+=strlen($pop1);
~ $pattern='`'.preg_quote($pop1).'`';
~ while(!preg_match($pattern,strip_tags($pop2))){
~ // if pop1 and pop2 are not referencing the same element
~ $pop2=array_pop($chk2);
~ $retVal.=$pop2.' ';
~ if($pop2==NULL) break;
~ }
~ }
~ return $retVal;
}

It wraps plain text or HTML as it displays (doesn't count the HTML tags
when processing). The string is split on whitespace, if there is a piece
that is longer than the wrap, it outputs just that as it was input.

HTH

- --
Justin Koivisto - ju****@koivi.com
http://www.koivi.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFB2tvhm2SxQ7JEbpoRAoeZAJ9XilXn/AgPBSpdAQe6ihCvpAQKqQCdFVkt
BkTpkYMcJYqJxA8R552govQ=
=IcB0
-----END PGP SIGNATURE-----
Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.