[SOLVED] Limiting tex outputs

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
Archy
Forum Contributor
Posts: 129
Joined: Fri Jun 18, 2004 2:25 pm
Location: USA

Limiting tex outputs

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

[php_man]substr[/php_man]

viewtopic.php?t=25351 may help too..
Archy
Forum Contributor
Posts: 129
Joined: Fri Jun 18, 2004 2:25 pm
Location: USA

Post 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>");
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

nl2br the processed content, then add in the table cell code.
Archy
Forum Contributor
Posts: 129
Joined: Fri Jun 18, 2004 2:25 pm
Location: USA

Post 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.
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Code: Select all

echo nl2br(preg_match('#^\s*(.{500,}?)\s+.*$#s', $message, $match)); // might work
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

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