Page 1 of 1
Split long text string
Posted: Thu Sep 30, 2004 5:11 am
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.
Posted: Thu Sep 30, 2004 5:52 am
by patrikG
What code have you got so far?
Posted: Thu Sep 30, 2004 9:29 am
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.
Posted: Thu Sep 30, 2004 9:45 am
by pickle
Try [php_man]wordwrap[/php_man]
Posted: Thu Sep 30, 2004 10:21 am
by Joe
Or you could mabey try
chunk_split()
Posted: Fri Oct 01, 2004 8:31 am
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.

Posted: Fri Oct 01, 2004 9:55 am
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";
}
Posted: Fri Oct 01, 2004 3:59 pm
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?
Posted: Sat Oct 02, 2004 6:49 am
by johndoe132
Bump!