Split long text string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
johndoe132
Forum Newbie
Posts: 13
Joined: Thu Sep 30, 2004 5:09 am

Split long text string

Post by johndoe132 »

Hi all,
I'm creating a program that allows a user to input a large amount of text (a letter) into a textarea field. When this is submitted the text is inserted into table cell in a letter template. The problem I have is splitting up the text to make it flow onto the next printable page.
Idealy I want to split the string in the variable into smaller chunks, but keeping line / word format (perhaps to an array?) then count the small chunks so I can issue page numbers as the pages are built.
Hope this all makes sense, and hope someone can help!
Cheers.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

What code have you got so far?
johndoe132
Forum Newbie
Posts: 13
Joined: Thu Sep 30, 2004 5:09 am

Post by johndoe132 »

Not a lot so far, just the contents of the textarea as a variable. I've tried several approaches such as forcing line breaks in the textarea box itself then counting the lines, but these never match up on the letter template. I've also tried counting the characters in the variable, but if I insert a page break after a certain number of characters, it will split words in half!
Hope this is clearer.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Try [php_man]wordwrap[/php_man]
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Or you could mabey try chunk_split()
johndoe132
Forum Newbie
Posts: 13
Joined: Thu Sep 30, 2004 5:09 am

Post by johndoe132 »

OK, I'm getting somewhere now.

Code: Select all

$letter=$_POST[letter];
$letter1=nl2br(stripslashes($letter));
$letter2 = wordwrap($letter1, 100, "<br />\n");
That code accepts the text from the textarea as a long string, with no line breaks, and formats it to look just like it was entered in the textarea box, with a maximum of 100 characters to a line by inserting <br /> tags.
I now need to set each line as an array element, which I am doing using:

Code: Select all

$myArray = explode("<br />", $letter2);
The problem comes when I try to print the text, as strange line breaks turn up, cutting lines in half etc. THis is the code I have so far:

Code: Select all

$display = 20; //number of lines to a page.
$start = ($page * $display) - $display;// page is set to 1, then increments for each added page.
$news = array_slice($myArray, $start, $display);

foreach($news as $key=>$value)
{
$value=nl2br($value);
    print("$value");
}
Any ideas would be a great help. :)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Remove all the newline characters until they're absolutely necessary.

Code: Select all

$letter=$_POST[letter];
$letter1=nl2br(stripslashes($letter));//this line is superfluous if you use wordwrap
$letter2 = wordwrap($letter1, 100, "|::|");//the key here is to have a unique delimiter
.
.
.
$myArray = explode("|::|", $letter2);
.
.
.
$display = 20; //number of lines to a page.
$start = ($page * $display) - $display;// page is set to 1, then increments for each added page.
$news = array_slice($myArray, $start, $display);

foreach($news as $key=>$value)
{
     echo $value."<br />\n";
}
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
johndoe132
Forum Newbie
Posts: 13
Joined: Thu Sep 30, 2004 5:09 am

Post by johndoe132 »

Thanks for the suggestion. It's still not quite right though. Line breaks are appearing where I don't want them and otheres don't appear where they should be. Would it help if I whip up an online example with the source code?
johndoe132
Forum Newbie
Posts: 13
Joined: Thu Sep 30, 2004 5:09 am

Post by johndoe132 »

Bump!
Post Reply