counting lines from a textarea [SOLVED]

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
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

counting lines from a textarea [SOLVED]

Post 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?
Last edited by gurjit on Wed May 09, 2007 10:52 am, edited 1 time in total.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post 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.
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post 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
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post 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.
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post by blackbeard »

Try using the word wrap function first, then count the number of newlines.
User avatar
dhrosti
Forum Commoner
Posts: 90
Joined: Wed Jan 10, 2007 5:01 am
Location: Leeds, UK

Post 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!
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post 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.
User avatar
dhrosti
Forum Commoner
Posts: 90
Joined: Wed Jan 10, 2007 5:01 am
Location: Leeds, UK

Post 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...
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post 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.

?>
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

You know that only works for fixed-width fonts, right?
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post 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
Post Reply