liamloveslearning Posted September 16, 2010 Share Posted September 16, 2010 Hi all, im trying to shorten this string which cnotains about 1000 characters, to say 250 <?php echo $row_Best_Sellers['experience_name']; ?> Ive tried using the below to no avail, can anybody give me some advice? <?PHP $small = some_function($row_Best_Sellers['experience_description']); echo $small; function some_function($string){ $string = substr($string,0,100); $string = substr($string,0,strrpos($string," ")); return $string; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/213557-shorten-string-nightmare/ Share on other sites More sharing options...
liamloveslearning Posted September 16, 2010 Author Share Posted September 16, 2010 I've also tried to no avail. <?php $text = 'something massivvveeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'; function ShortenText($text) { $chars = 25; $text = $text." "; $text = substr($text,0,$chars); $text = substr($text,0,strrpos($text,' ')); $text = $text."..."; return $text; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/213557-shorten-string-nightmare/#findComment-1111617 Share on other sites More sharing options...
ignace Posted September 16, 2010 Share Posted September 16, 2010 What do you get as output? $string = substr($string,0,100); $string = substr($string,0,strrpos($string," ")); Should give you an output of less then 100 characters (unless the last character is a space of course). The below should give you the desired output. function some_function($string, $max_avg_length = 250) { return substr($string, 0, (isset($string[$max_avg_length]) ? strrpos($string, ' ', $max_avg_length) : 0)); } Quote Link to comment https://forums.phpfreaks.com/topic/213557-shorten-string-nightmare/#findComment-1111618 Share on other sites More sharing options...
liamloveslearning Posted September 16, 2010 Author Share Posted September 16, 2010 Thanks for your response ignace, I managed to get it working using <?php echo substr($row_Best_Sellers['experience_description'],0,80)."..."; ?> but now im cropping my paragraph in the middle fo words, is there a way to exclude this? Quote Link to comment https://forums.phpfreaks.com/topic/213557-shorten-string-nightmare/#findComment-1111620 Share on other sites More sharing options...
ignace Posted September 16, 2010 Share Posted September 16, 2010 Thanks for your response ignace, I managed to get it working using <?php echo substr($row_Best_Sellers['experience_description'],0,80)."..."; ?> but now im cropping my paragraph in the middle fo words, is there a way to exclude this? echo isset($row_Best_Sellers['experience_description'][90]) // make sure there is something after the 80th character // cut the string off at the space preceding the 80th character ? substr($row_Best_Sellers['experience_description'], 0, strrpos($row_Best_Sellers['experience_description'], ' ', 80)) . '...' : $row_Best_Sellers['experience_description']; // otherwise just print it Quote Link to comment https://forums.phpfreaks.com/topic/213557-shorten-string-nightmare/#findComment-1111728 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.