Page 1 of 1
counting lines from a textarea [SOLVED]
Posted: Wed May 09, 2007 9:31 am
by gurjit
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?
Posted: Wed May 09, 2007 9:35 am
by Grim...
Code: Select all
<?php
$lines = explode("\n", $text);
for ($i = 0, $i <= 4; $i ++) {
print $lines[$i]."<br />";
}
?>
There's probably a nicer solution that uses regex, but I suck at regex.
Posted: Wed May 09, 2007 9:51 am
by gurjit
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
Posted: Wed May 09, 2007 9:53 am
by Grim...
Oh right - got you.
The only real way to do that is to use a fixed-width font and work it out mathematically.
As PHP is run before the page is rendered it can't be of much help to you.
Posted: Wed May 09, 2007 10:02 am
by blackbeard
Try using the word wrap function first, then count the number of newlines.
Posted: Wed May 09, 2007 10:11 am
by dhrosti
i think there's a function called wordwrap($x) or something, that produces a new line after $x characters, allowing you to explode("\n", $text) into an array of lines.
Edit: black beard got there first!
Posted: Wed May 09, 2007 10:14 am
by gurjit
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.
Posted: Wed May 09, 2007 10:31 am
by dhrosti
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...
Posted: Wed May 09, 2007 10:50 am
by gurjit
THANKS dhrosti (took your solution and added some extra)
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.
?>
Posted: Wed May 09, 2007 1:00 pm
by Grim...
You know that only works for fixed-width fonts, right?
Posted: Fri May 11, 2007 4:26 am
by gurjit
You know that only works for fixed-width fonts, right?
Anyone using this code.... this is true. If you have fixed-widths and fonts the above code will work.
I have letters with fixed font and width and it worked for me.
Thanks to ALL for your contributions