Splitting long text

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


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-24-2007
Skip Evans
 
Posts: n/a
Default Splitting long text

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/
Reply With Quote
  #2 (permalink)  
Old 01-24-2007
Paul Novitski
 
Posts: n/a
Default Re: [PHP] Splitting long text

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
Reply With Quote
  #3 (permalink)  
Old 01-24-2007
Anas Mughal
 
Posts: n/a
Default Re: [PHP] Splitting long text

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

Reply With Quote
  #4 (permalink)  
Old 01-24-2007
Travis Doherty
 
Posts: n/a
Default Re: [PHP] Splitting long text

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
Reply With Quote
  #5 (permalink)  
Old 01-24-2007
Paul Novitski
 
Posts: n/a
Default Re: [PHP] Splitting long text

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
Reply With Quote
Reply


Thread Tools
Display Modes

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

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



All times are GMT +1. The time now is 05:37 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0