Connecting Tech Pros Worldwide Help | Site Map

php word wrap function needed - one that works

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 10:29 AM
leegold2
Guest
 
Posts: n/a
Default php word wrap function needed - one that works

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.

  #2  
Old July 17th, 2005, 10:29 AM
jblanch
Guest
 
Posts: n/a
Default Re: php word wrap function needed - one that works

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.

  #3  
Old July 17th, 2005, 10:29 AM
leegold2
Guest
 
Posts: n/a
Default Re: php word wrap function needed - one that works

jblanch wrote:
[color=blue]
> 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.[/color]

I don't know if I can modfiy this to ignore links...[color=blue]
>[/color]
  #4  
Old July 17th, 2005, 10:29 AM
Janwillem Borleffs
Guest
 
Posts: n/a
Default Re: php word wrap function needed - one that works

leegold2 wrote:[color=blue]
> I don't know if I can modfiy this to ignore links...[/color]

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



  #5  
Old July 17th, 2005, 10:29 AM
Manuel Lemos
Guest
 
Posts: n/a
Default Re: php word wrap function needed - one that works

Hello,

on 01/02/2005 03:58 AM leegold2 said the following:[color=blue]
> 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,[/color]

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
  #6  
Old July 17th, 2005, 10:29 AM
Janwillem Borleffs
Guest
 
Posts: n/a
Default Re: php word wrap function needed - one that works

leegold2 wrote:[color=blue]
> I don't know if I can modfiy this to ignore links...[/color]

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



  #7  
Old July 17th, 2005, 10:31 AM
Justin Koivisto
Guest
 
Posts: n/a
Default Re: php word wrap function needed - one that works

-----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 - justin@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-----
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.