shadiadiph Posted May 11, 2009 Share Posted May 11, 2009 mm not sure how to explain this but here goes. Say I have a string $subject which could be 5 or 100 characters in length but i never want to display more than the first 30 characters of it how would i do this? Link to comment https://forums.phpfreaks.com/topic/157658-solved-string-display-length/ Share on other sites More sharing options...
Ken2k7 Posted May 11, 2009 Share Posted May 11, 2009 substr Link to comment https://forums.phpfreaks.com/topic/157658-solved-string-display-length/#findComment-831341 Share on other sites More sharing options...
nblasgen Posted May 11, 2009 Share Posted May 11, 2009 define('MAX_SIZE', 30); if (strlen($subject) > MAX_SIZE) { echo substr($subject, 0, MAX_SIZE - 3) . '...'; } else { echo $subject; } Link to comment https://forums.phpfreaks.com/topic/157658-solved-string-display-length/#findComment-831403 Share on other sites More sharing options...
Ken2k7 Posted May 11, 2009 Share Posted May 11, 2009 Why MAX_SIZE - 3? Link to comment https://forums.phpfreaks.com/topic/157658-solved-string-display-length/#findComment-831406 Share on other sites More sharing options...
DarkSuperHero Posted May 11, 2009 Share Posted May 11, 2009 he probably did MAX_SIZE - 3 so that including the '...' it would not exceed the total number of desired characters... just a guess though... Link to comment https://forums.phpfreaks.com/topic/157658-solved-string-display-length/#findComment-831416 Share on other sites More sharing options...
shadiadiph Posted May 11, 2009 Author Share Posted May 11, 2009 thank you I had to use to get it to display the first 36 characters of the string $subject2 = substr($subject,0,36); Link to comment https://forums.phpfreaks.com/topic/157658-solved-string-display-length/#findComment-831418 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.