Page 1 of 1

Limiting tex outputs

Posted: Thu Sep 09, 2004 6:10 pm
by Archy
I know that this can be done, but cant find it on PHP.net or on google. Basically I want to output some writing, but limit it to say 500 words, so it will stop outputting the text after 500 words (more likely characters than words!).

The reason for this being that some of the entries are quite long, and so will take up too much space on the front page.

Thanks.

Posted: Thu Sep 09, 2004 6:14 pm
by feyd
[php_man]substr[/php_man]

viewtopic.php?t=25351 may help too..

Posted: Thu Sep 09, 2004 6:26 pm
by Archy
Ok, thanks, but, how would I use that along with the echo nl2br code, when I add it, it keeps coming out with error's : (

Code: Select all

echo nl2br("<td>".$row['content']."</td>");

Posted: Thu Sep 09, 2004 6:28 pm
by feyd
nl2br the processed content, then add in the table cell code.

Posted: Thu Sep 09, 2004 6:32 pm
by Archy
Hmmm, I am still coming out with an error... well, technically not an error, just displays it non-differently. This is the code I am using at the moment, think you could lend me a hand ?

[EDIT] Solved, thanks.

Posted: Fri Sep 10, 2004 12:39 am
by ol4pr0

Code: Select all

echo nl2br(preg_match('#^\s*(.{500,}?)\s+.*$#s', $message, $match)); // might work

Posted: Fri Sep 10, 2004 12:32 pm
by timvw
If the data is coming from mysql, it's probably more efficient to fetch only what you need. The following query would limit the content to 20 "words".

Code: Select all

SELECT  SUBSTRING_INDEX(content,' ',20) as intro FROM items