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!
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.
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
}
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.
$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.