Page 1 of 2
line counter
Posted: Thu Mar 18, 2004 5:40 am
by gurjit
hi all,
if i had a variable lets say:
$var =
" i want to be able to count the number of lines i have in this sentence."
my table in the html was like this
<table width="10">
</table>
i want to be able to count the number of words that can fit in a table of width="10". if the word does not fit then put it on a new line. then find the number of lines i have in $var.
how can i do this.
my display at the end would look like this:
<body>
<table width="10" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
i want to be
able to
count the
number of
lines i
have in
this
sentence."</td>
</tr>
</table>
so i have 8 lines and all the words fit perfect on the line according to my width?
Posted: Thu Mar 18, 2004 5:43 am
by JAM
Perhaps you could use [php_man]wordwrap[/php_man]() instead of manually do the counting?
Posted: Thu Mar 18, 2004 5:46 am
by gurjit
how can i count the number of lines in the word wrap?
Posted: Thu Mar 18, 2004 5:56 am
by JAM
You cant, but you can use wordwrap to wrap lines at the same size as your <table width="X">. Just a thought...
More ideas are to look into the use of functions like strpos ( strpos($string, " ") ), strlen, substr, substr_count and so on...
Posted: Thu Mar 18, 2004 5:59 am
by gurjit
can i count the number of characters in the string then rather than lines?
Posted: Thu Mar 18, 2004 6:03 am
by JAM
Just an example...
Code: Select all
<?php
$string = ' // 1
This // 2
is a // 3
string.';
echo substr_count($string, "\n");
The manual can be found here:
http://www.php.net/
Posted: Thu Mar 18, 2004 8:28 am
by gurjit
how do i find the number of lines in this, everything in "" are dynamic:
Dear "SALUTATION",
We are pleased that "ALL_SIBLIN" "HAS_HAVE" been accepted for a place at the School "CENTRE" school, which holds classes as outlined on the enclosed page. Please review the information carefully and let us know of any errors or alterations as soon as possible.
The fees due for the "TERM_NAME" term are $"FEES_DUE_T". Payment of the balance, due by the third week of term, may be made by returning the attached Remittance Advice and your check made payable to School. If you would prefer to pay in two installments please return the attached Remittance Advice together with two checks. (If you use this option, there is a small charge to cover additional administration costs.)
School colors are black and yellow and by the third week of class students are required to wear suitable clothing such as: leggings, shorts, sweat pants, T-shirt or sweat shirt, etc., in the school colors. Also, jazz or ballet shoes are compulsory footwear. uniform bearing the school logo is available and we have enclosed a uniform order form for your convenience. Students attending the Main School should bring a light snack
Yours Sincerely
Posted: Thu Mar 18, 2004 8:33 am
by patrikG
PHP Manual wrote:
wordwrap
Wraps a string to a given number of characters using a string break character. (PHP 4 >= 4.0.2)
string wordwrap ( string str [, int width [, string break [, boolean cut]]] )
Returns a string with str wrapped at the column number specified by the optional width parameter. The line is broken using the (optional) break parameter.
wordwrap() will automatically wrap at column 75 and break using '\n' (newline) if width or break are not given.
If the cut is set to 1, the string is always wrapped at the specified width. So if you have a word that is larger than the given width, it is broken apart. (See second example).
Basically, wordwrap either adds a \n at the end of each line or a custom line-delimiter. If you simply explode the string by either \n or you custom delimiter you end up with an array containing the number of lines.
Posted: Thu Mar 18, 2004 8:36 am
by gurjit
i tried this but gives wrong answer. i'm going to put the text in a table of width=\"670\". thats where i need to find the number of lines. in wordwrap you can only give the number of characters.
Code: Select all
<?php
$text = "Dear "SALUTATION",
We are pleased that "ALL_SIBLIN" "HAS_HAVE" been accepted for a place at the School "CENTRE" school, which holds classes as outlined on the enclosed page. Please review the information carefully and let us know of any errors or alterations as soon as possible.
The fees due for the "TERM_NAME" term are $"FEES_DUE_T". Payment of the balance, due by the third week of term, may be made by returning the attached Remittance Advice and your check made payable to School. If you would prefer to pay in two installments please return the attached Remittance Advice together with two checks. (If you use this option, there is a small charge to cover additional administration costs.)
School colors are black and yellow and by the third week of class students are required to wear suitable clothing such as: leggings, shorts, sweat pants, T-shirt or sweat shirt, etc., in the school colors. Also, jazz or ballet shoes are compulsory footwear. uniform bearing the school logo is available and we have enclosed a uniform order form for your convenience. Students attending the Main School should bring a light snack
Yours Sincerely";
$mystr2 = str_replace(chr(13),"<br />",$text);
$counter2 = substr_count($mystr2, "<br />");
$mystr = wordwrap($text, 112, "<br />",0);
$counter = substr_count($mystr, "<br />");
$tot = $counter + $counter2;
echo "counter = $counter <br>counter2 = $counter2 <p></p>string counter: ". $tot;
echo "
<table width="670" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>". str_replace(chr(13),"<br>",$text) . "</td>
</tr>
</table>
";
?>
Posted: Thu Mar 18, 2004 8:49 am
by patrikG
You'd need a clientside solution for that, DHTML that is where you create a <div> and calculate it's height, divide it by the font-size (e.g. the font is 12px, so you divide it by 12) and that would give you the desired result.
Posted: Thu Mar 18, 2004 8:53 am
by gurjit
i more concerned with the line count than the display.
what do you mean by div count patrikG. i want to know how many lines are in the $text when it displays in the table wisth=670
this line shows me line numbers at the moment
Code: Select all
<?php
echo "counter = $counter <br>counter2 = $counter2 <p></p>string counter: ". $tot;
?>
Posted: Thu Mar 18, 2004 8:58 am
by patrikG
gurjit wrote:i more concerned with the line count than the display.
what do you mean by div count patrikG. i want to know how many lines are in the $text when it displays in the table wisth=670
I mean that <table width="670"> is HTML, i.e. clientside, which has nothing to do with PHP in terms of functionality. That is why I am saying that you need a client-side solution as a HTML table-property is simple not PHP (i.e. serverside), but clientside.
By <div> I mean what used to be called "layer".
Alternatively, you can measure how many characters there are per line in a table with width="670". Divide the total number of characters and calculate your total line-number (bear in mind \n\r etc).
Posted: Thu Mar 18, 2004 9:00 am
by gurjit
not sure how to go about that. i would appreciate if you could write me some code to do this. i am very lost and need a solution immediately
Posted: Thu Mar 18, 2004 9:02 am
by patrikG
I think I pointed you in the right direction, think about your options, the options I've outlined and use google to research those you consider best, or use a book.
I don't want to sound too harsh, but I am not here to do your job for you.
Posted: Thu Mar 18, 2004 10:39 am
by gurjit
does no one have an answer to this problem. i've been at it all day and cant figure it out. please someone HELP.