counting lines from a textarea [SOLVED]
Moderator: General Moderators
counting lines from a textarea [SOLVED]
Hi
If a user enters text in a <textarea></textarea> and submits the form. How can i get php too count the number of lines displayed in lets say a <table width="650" height="975" border="0" cellpadding="0" cellspacing="0">
What I'm trying to achieve is:
I have a textarea where users type a max of 20 lines. The user clicks the submit button after entering the text, which might include new paragraph breaks. The text entered gets displayed at the end of a static letter, which has the table dimensions above. I need to display 5 lines on page 1 and then break the page and display the remaning lines on page 2 (this is for printing).
How can I count five lines? The user can enter anything upto 20 lines?
If a user enters text in a <textarea></textarea> and submits the form. How can i get php too count the number of lines displayed in lets say a <table width="650" height="975" border="0" cellpadding="0" cellspacing="0">
What I'm trying to achieve is:
I have a textarea where users type a max of 20 lines. The user clicks the submit button after entering the text, which might include new paragraph breaks. The text entered gets displayed at the end of a static letter, which has the table dimensions above. I need to display 5 lines on page 1 and then break the page and display the remaning lines on page 2 (this is for printing).
How can I count five lines? The user can enter anything upto 20 lines?
Last edited by gurjit on Wed May 09, 2007 10:52 am, edited 1 time in total.
Code: Select all
<?php
$lines = explode("\n", $text);
for ($i = 0, $i <= 4; $i ++) {
print $lines[$i]."<br />";
}
?>This code does not detect non manual <p> breaks.
for example
"If this was the text and I wanted it to start a new line after the table width had reached 650px, it will not pick it up. How ever if I put a paragraph break by hitting the return key, then it would count it as a new line break.
This paragraph break will be line 2"
The code would say there are only 2 lines because the <p> is counted as a second line.
I want to see how many lines there are in a table with dimensions as below. So for example it
<table width="650" height="975" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
If this was the text and I wanted it to start a new line after the table width had reached 650px, it will not pick
it up. How ever if I put a paragraph break by hitting the return key, then it would count it as a new line
break.
This paragraph break will be line 2
</td>
</tr>
</table>
so there are 4 lines and not 2
for example
"If this was the text and I wanted it to start a new line after the table width had reached 650px, it will not pick it up. How ever if I put a paragraph break by hitting the return key, then it would count it as a new line break.
This paragraph break will be line 2"
The code would say there are only 2 lines because the <p> is counted as a second line.
I want to see how many lines there are in a table with dimensions as below. So for example it
<table width="650" height="975" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
If this was the text and I wanted it to start a new line after the table width had reached 650px, it will not pick
it up. How ever if I put a paragraph break by hitting the return key, then it would count it as a new line
break.
This paragraph break will be line 2
</td>
</tr>
</table>
so there are 4 lines and not 2
-
blackbeard
- Forum Contributor
- Posts: 123
- Joined: Thu Aug 03, 2006 6:20 pm
when you mean the word wrap, are you suggesting that I make the textarea the same width as the letter. When the user clicks submit, use javascript to count the lines and pass that variable through into the PHP. (It still wont be able to tell at what point to break the page)
I think I will have to just count the characters and use PHP too calculate roughly where a line ends and where to break the page.
Any ideas/thoughts of how too get around this will be appreciated.
I think I will have to just count the characters and use PHP too calculate roughly where a line ends and where to break the page.
Any ideas/thoughts of how too get around this will be appreciated.
wordwrap will insert a "\n" after a defined number of characters, so you can explode the text around the "\n"'s and you will have each line as a key in an array...
Code: Select all
$text = $_POST['text'];
$newText = wordwrap($text, 20, "\n");
$parts = explode("\n", $newText);
// first 5 lines...
$parts[0]; // line 1
$parts[1]; // line 2
// and so on...THANKS dhrosti (took your solution and added some extra)
heres the solution to find the number of lines in my table width
heres the solution to find the number of lines in my table width
Code: Select all
<?php
$limitedtextarea = $_POST['txt_entered'];
$limitedtextarea = wordwrap($limitedtextarea, 80, "<br>");
$limitedtextarea = str_replace(chr(13),"<br>",$limitedtextarea);
$lines = explode( "<br>", $limitedtextarea);
$count = count( $lines );
echo $limitedtextarea.
?>