dreamwest Posted March 4, 2011 Share Posted March 4, 2011 Is it possible to get substr to end on a word boundry instead of halfway through a word? $txt = "The cat goes splat on my bumper mat"; echo substr($txt, 0, 120); //this can be around 12 chars so doesnt need to be exactly 12 chars Quote Link to comment https://forums.phpfreaks.com/topic/229555-substr/ Share on other sites More sharing options...
Muddy_Funster Posted March 4, 2011 Share Posted March 4, 2011 not sure if it's what you are looking for, bur strrpos() -- http://www.php.net/manual/en/function.strrpos.php -- should be able to help. Quote Link to comment https://forums.phpfreaks.com/topic/229555-substr/#findComment-1182690 Share on other sites More sharing options...
menator Posted March 4, 2011 Share Posted March 4, 2011 Here is a function I found with a search function WordLimiter($text,$limit=20){ $explode = explode(' ',$text); $string = ''; $dots = '...'; if(count($explode) <= $limit){ $dots = ''; } for($i=0;$i<$limit;$i++){ $string .= $explode[$i]." "; } if ($dots) { $string = substr($string, 0, strlen($string)); } return $string.$dots; } // Usage $str = 'This is a long string. Could be longer too.'; echo WordLimiter($str,4); Quote Link to comment https://forums.phpfreaks.com/topic/229555-substr/#findComment-1182693 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.