how to wordrap to 30 character and 2 rows long?

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
Antek_Ryu
Forum Commoner
Posts: 34
Joined: Tue Aug 09, 2005 10:55 am

how to wordrap to 30 character and 2 rows long?

Post 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

Code: Select all

wordwrap($text, 30, "<br />");
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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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
    
}
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

You don't need to explode:

Code: Select all

function wrapText($string)
{  
    return wordwrap($string, '30', "<br />\n");  
}
:)
Antek_Ryu
Forum Commoner
Posts: 34
Joined: Tue Aug 09, 2005 10:55 am

Post 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] : ''));
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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");

?>
Antek_Ryu
Forum Commoner
Posts: 34
Joined: Tue Aug 09, 2005 10:55 am

Post 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???
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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 />');
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 :D

If there were already line breaks in the original text, we can still only have 2 rows... yours allows more rows of text ;)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

And would also chop the word if it happens to over step the 60char limit. :)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 :lol:
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: how to wordrap to 30 character and 2 rows long?

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