PHP limiting output
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
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
ROB posted this at 03:22 — 2nd May 2003.
They have: 447 posts
Joined: Oct 1999
this should work
SELECT CONCAT(SUBSTRING(fieldname, 0, 100), '...') AS fieldname FROM tablename;
ROB posted this at 03:29 — 2nd May 2003.
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 posted this at 11:09 — 2nd May 2003.
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.
mizzy posted this at 23:56 — 2nd May 2003.
They have: 47 posts
Joined: Jun 2001
Thanks ...works perfectly
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.