Page 1 of 1
how to wordrap to 30 character and 2 rows long?
Posted: Thu Oct 27, 2005 4:49 pm
by Antek_Ryu
Hi
I am using the function wordwrap for the following scenario. I am getting text from a database which needs to go within a html table cell. The cell can hold only 30 character per line and only on a maximum of two lines. The problem is I dont know how much text is kept in the database and in most cases it will be more than 60 characters. If I use this function
This will wordwrap my text from the database in the html table cell and in most cases go over 2 rows. How can I limit the number of words to go into 2 rows only and have no words cut on the second row.
Hope I made sense
thanks
Antek
Posted: Thu Oct 27, 2005 6:15 pm
by Chris Corbyn
Just break it into seperate lines and then take the first two
Code: Select all
function wrapText($string)
{
$wrapped = wordwrap($string, '30', "\n");
$lines = explode("\n", $wrapped); //Break into single lines
return nl2br($lines[0]."\n".(isset($lines[1]) ? $lines[1] : '')); //Stick the first two together
}
Posted: Fri Oct 28, 2005 1:39 am
by Jenk
You don't need to explode:
Code: Select all
function wrapText($string)
{
return wordwrap($string, '30', "<br />\n");
}

Posted: Fri Oct 28, 2005 5:00 am
by Antek_Ryu
I have used the code that was given but it only returns the first character of the first line and the first character of the second line?
Why does it do that I have pasted my code below
Code: Select all
$news= $row['News'];
$news = trim($news);
$news = wordwrap($news, '30', "<br />\n");
$news = nl2br($news[0]."\n".(isset($news[1]) ? $news[1] : ''));
Posted: Fri Oct 28, 2005 5:02 am
by Jenk
You don't need all of that, replace those 4 lines with the following:
Code: Select all
<?php
$news = wordwrap($row['news'], '30', "<br />\n");
?>
Posted: Fri Oct 28, 2005 5:18 am
by Antek_Ryu
But that then returns 5 lines for me. I just want 2 lines this is why i was using the code suggested below;
Code: Select all
$news = nl2br($news[0]."\n".(isset($news[1]) ? $news[1] : ''));
But when I use this line above it only returns the first character of each line i.e line 1 and line 2. Where is the rest of the text going???
Posted: Fri Oct 28, 2005 6:23 am
by Jenk
You want the first 2 lines max?
I thought you meant the field was limited to 60 char's maximum (dunno why I thought that) and needed to limit each line to 30.
In that case, use d11wtq's function.
Posted: Fri Oct 28, 2005 10:02 am
by pickle
This should work too:
Code: Select all
$formatted_string = wordwrap(substr($string,0,60),30,'<br />');
which is just a compressed version of
Code: Select all
$string = substr($string,0,60);
$formatted_string = wordwrap($string,30,'<br />');
Posted: Fri Oct 28, 2005 10:04 am
by Chris Corbyn
pickle wrote:This should work too:
Code: Select all
$formatted_string = wordwrap(substr($string,0,60),30,'<br />');
which is just a compressed version of
Code: Select all
$string = substr($string,0,60);
$formatted_string = wordwrap($string,30,'<br />');
Except for one important thing that I almost forgot
I was gonna let you think but I'll just say it
If there were already line breaks in the original text, we can still only have 2 rows... yours allows more rows of text

Posted: Fri Oct 28, 2005 10:13 am
by Jenk
And would also chop the word if it happens to over step the 60char limit.

Posted: Fri Oct 28, 2005 10:35 am
by pickle
d11wtq wrote:If there were already line breaks in the original text, we can still only have 2 rows... yours allows more rows of text
Well don't you feel big.. Mr "I think of everything"
That's a good point.
Posted: Fri Oct 28, 2005 10:41 am
by Chris Corbyn
pickle wrote:d11wtq wrote:If there were already line breaks in the original text, we can still only have 2 rows... yours allows more rows of text
Well don't you feel big.. Mr "I think of everything"
That's a good point.
LMFAO

Re: how to wordrap to 30 character and 2 rows long?
Posted: Fri Oct 28, 2005 10:50 am
by RobertGonzalez
Antek_Ryu wrote:I am getting text from a database which needs to go within a html table cell. The cell can hold only 30 character per line and only on a maximum of two lines. The problem is I dont know how much text is kept in the database and in most cases it will be more than 60 characters.
Take care of this at the DB level...
Code: Select all
$query = "SELECT LEFT(`fieldname`, 60) FROM tbl_name";
Then throw this into your script wordwrap() funtion call. That way you will always only have 60 characters to work with. This example assumes you are using MySQL. I think SQL server and Access have functions similar to this (mid() I think). Anyway, hope this helps even a little bit.