Web Hosting Talk







View Full Version : Limiting Characters in PHP


SmashedPumpkins
11-25-2009, 07:05 PM
I'm pulling a huge description from a database and I'd like to show only the first 150 characters or so. After of which it'll have a view more button. I have the code, but it may be a tad confusing to look at. It's looping data that is put into arrays. The description is "rex_details".

<?php
foreach($rows as $row)
{
printf ("
<div class='member_tab'> <a href='/dates.php?id=%s'><img src='%s' heigth='78' width='108' class='photo' /></a>
<div class='member_details'> <a href='/dates.php?id=%s' class='linkfix'><span>%s</span></a><br />
<p>%s</p>
<a href='/dates.php?id=%s' class='more'>more</a>
</div>
</div>
</div>
",$row['rex_id'],$row['rex_url'],$row['rex_id'],$row['rex_title'],$row['rex_details'],$row['rex_id']);
}
?>

squirrelhost
11-25-2009, 07:10 PM
http://www.the-art-of-web.com/php/truncate/

or check out SELECT SUBSTRING_INDEX
in mysql docs

FreeKill
11-25-2009, 09:46 PM
PHP also has a bunch of different functions that might do what you want...
http://www.php.net/manual/en/function.chunk-split.php
http://php.net/manual/en/function.wordwrap.php

SmashedPumpkins
11-25-2009, 10:26 PM
Thanks everyone :) Got a working one!