PHP limiting output

They have: 47 posts

Joined: Jun 2001

Hi
Does anyone know a PHP or command routine to limit database output characters? ...say mySQL database field has 200 characters and I only need to display 100 with ...at the end. How do I do it
Thanks
Mizzy

They have: 447 posts

Joined: Oct 1999

this should work

SELECT CONCAT(SUBSTRING(fieldname, 0, 100), '...') AS fieldname FROM tablename;
'

They have: 447 posts

Joined: Oct 1999

or if you want a php function i use something like this

<?php
function strshorten($instr,$len) {
   
$instr = trim($instr);
    if(
strlen($instr) > $len) $instr = substr($instr, 0, $len) . '...';
    return
$instr;
}

$string = 'this string has 29 characters';
echo
strshorten($string, 16);
// prints 'this string has...'
?>

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

I would very much recommend the first solution (SQL) over the second (PHP). This will occupy less memory, and may be a bit faster once the recordset is returned to PHP for further processing.

Mark Hensler
If there is no answer on Google, then there is no question.

They have: 47 posts

Joined: Jun 2001

Thanks ...works perfectly Laughing out loud

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.