Limit # of characters
Moderator: General Moderators
Limit # of characters
Hi,
I want to limit the number of characters when displaying a record. For example, if the record is 40 characters long I want to stop it at 20 and add "..." at the end because some URLs can be very long.
How would I do that?
I want to limit the number of characters when displaying a record. For example, if the record is 40 characters long I want to stop it at 20 and add "..." at the end because some URLs can be very long.
How would I do that?
Code: Select all
$txt = (strlen($txt) > 40) ? substr($txt,0,20).'...' : $txt;-
Charles256
- DevNet Resident
- Posts: 1375
- Joined: Fri Sep 16, 2005 9:06 pm
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Code: Select all
function chopLink($url, $length='20') {
return strlen($url >= $length) ? '<a href="'.$url.'">'.substr($url,0,$length).'...</a>' : '<a href="'.$url.'">'.$url'</a>';
}-
Charles256
- DevNet Resident
- Posts: 1375
- Joined: Fri Sep 16, 2005 9:06 pm
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
I often use ternary for setting default variables.
Often helps with unitialized variables
Code: Select all
$var = isset($_GET['pagenum']) : $_GET['pagenum'] : 0;No problem, vchris. 
(my own code, anyway)
@jcart: I've just gotten into a habit of using isset() or empty().
Haha, I can read code near as easily as I can English.Charles256 wrote:i'm obsessed with reading my file as ifit were enlish:-D so I RARELY use the ternary operand. but i guess it's all personal preference.
@jcart: I've just gotten into a habit of using isset() or empty().