This is a discussion on Splitting long text within the PHP General forums, part of the PHP Programming Forums category; Hey all, I have a requirement to take a large amount of text, a story submitted to a competition, and ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hey all,
I have a requirement to take a large amount of text, a story submitted to a competition, and split into displayable chunks of 600 words each. I'd like some feedback on the best way to this. Thanks! Skip -- Skip Evans Big Sky Penguin, LLC 61 W Broadway Butte, Montana 59701 406-782-2240 http://bigskypenguin.com =-=-=-=-=-=-=-=-=-= Check out PHPenguin, a lightweight and versatile PHP/MySQL development framework. http://phpenguin.bigskypenguin.com/ |
|
|||
|
At 1/23/2007 05:52 PM, Skip Evans wrote:
>I have a requirement to take a large amount of text, a story >submitted to a competition, and split into displayable chunks of 600 >words each. You can explode a text into an array of words, slice off a 600-element array, and implode the result back into text. http://php.net/explode http://php.net/implode http://php.net/array-slice Regards, Paul __________________________ Juniper Webcraft Ltd. http://juniperwebcraft.com |
|
|||
|
I have been using the below function successfully. Hope it works for you.
-- Anas // textwrap 1.1 Brian Moon <brian@phorum.org> // This code is part of the Phorum project <http://phorum.org> // $String The string to be wrapped. // $breaksAt How many characters each line should be. // $breakStr What character should be used to cause a break. // $padStr Allows for the wrapped lines to be padded at the begining. function textwrap ($String, $breaksAt = 78, $breakStr = "\n", $padStr="") { $newString=""; $lines=explode($breakStr, $String); $cnt=count($lines); for($x=0;$x<$cnt;$x++){ if(strlen($lines[$x])>$breaksAt){ $str=$lines[$x]; while(strlen($str)>$breaksAt){ $pos=strrpos(chop(substr($str, 0, $breaksAt)), " "); if ($pos == false) { break; } $newString.=$padStr.substr($str, 0, $pos).$breakStr; $str=trim(substr($str, $pos)); } $newString.=$padStr.$str.$breakStr; } else{ $newString.=$padStr.$lines[$x].$breakStr; } } return $newString; } // end textwrap() On 1/23/07, Skip Evans <skip@bigskypenguin.com> wrote: > > Hey all, > > I have a requirement to take a large amount of > text, a story submitted to a competition, and > split into displayable chunks of 600 words each. > > I'd like some feedback on the best way to this. > > Thanks! > Skip > > -- > Skip Evans > Big Sky Penguin, LLC > 61 W Broadway > Butte, Montana 59701 > 406-782-2240 > http://bigskypenguin.com > =-=-=-=-=-=-=-=-=-= > Check out PHPenguin, a lightweight and > versatile PHP/MySQL development framework. > http://phpenguin.bigskypenguin.com/ > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- Anas Mughal |
|
|||
|
Anas Mughal wrote:
> I have been using the below function successfully. Hope it works for you. > -- Anas > > > > // textwrap 1.1 Brian Moon <brian@phorum.org> > // This code is part of the Phorum project <http://phorum.org> > // $String The string to be wrapped. > // $breaksAt How many characters each line should be. > // $breakStr What character should be used to cause a break. > // $padStr Allows for the wrapped lines to be padded at the begining. Short of string padStr in that function, and bool cut in the native PHP one, I don't see why someone wouldn't just use http://php.net/wordwrap/ . Travis Doherty |
|
|||
|
At 1/23/2007 05:52 PM, Skip Evans wrote:
>I have a requirement to take a large amount of text, a story >submitted to a competition, and split into displayable chunks of 600 >words each. At 1/23/2007 07:20 PM, Anas Mughal wrote: >// textwrap 1.1 Brian Moon <brian@phorum.org> >// This code is part of the Phorum project <http://phorum.org> >// $String The string to be wrapped. >// $breaksAt How many characters each line should be. >// $breakStr What character should be used to cause a break. At 1/23/2007 10:40 PM, Travis Doherty wrote: >Short of string padStr in that function, and bool cut in the native PHP >one, I don't see why someone wouldn't just use http://php.net/wordwrap/ . Ouch! Inserting hard carriage returns into the markup can only work when a) the font size is fixed (which we know better than to attempt) or b) the column width enlarges with the font size in a completely zoomable page. If the number of characters per line is allowed to change, as happens when font size changes in a fixed column width, hard carriage returns will break the wrap. Far better to let the browser handle word-wrap. Regards, Paul __________________________ Juniper Webcraft Ltd. http://juniperwebcraft.com |
![]() |
| Thread Tools | |
| Display Modes | |
|
|