Split long text string
Moderator: General Moderators
-
johndoe132
- Forum Newbie
- Posts: 13
- Joined: Thu Sep 30, 2004 5:09 am
Split long text string
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.
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.
-
johndoe132
- Forum Newbie
- Posts: 13
- Joined: Thu Sep 30, 2004 5:09 am
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.
Hope this is clearer.
Or you could mabey try chunk_split()
-
johndoe132
- Forum Newbie
- Posts: 13
- Joined: Thu Sep 30, 2004 5:09 am
OK, I'm getting somewhere now.
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:
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:
Any ideas would be a great help. 
Code: Select all
$letter=$_POST[letter];
$letter1=nl2br(stripslashes($letter));
$letter2 = wordwrap($letter1, 100, "<br />\n");I now need to set each line as an array element, which I am doing using:
Code: Select all
$myArray = explode("<br />", $letter2);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");
}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