gergy008 Posted February 16, 2011 Share Posted February 16, 2011 Hi guys, What is it called when you have a long paragraph shoretend, With php of course, To make it like haljh;lajbsdg... For example, I have this paragraph: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus placerat consequat enim, pretium viverra massa cursus nec. Sed in pharetra magna. Aliquam egestas vehicula mi, sed pellentesque ligula suscipit a. Donec lectus velit, luctus sed lobortis vitae, sollicitudin in sem. Ut facilisis, velit in accumsan porttitor, libero neque molestie leo, ac vulputate eros libero eget mauris. Ut nec justo augue. How to I make it so PHP detects if it is too long and trims it down like: Lorem ipsum dolor sit amet, consectetur adipiscing elit... If I can find what this is called then I can google it to find it and use it on my site. Thanks in advance! Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted February 16, 2011 Share Posted February 16, 2011 You'd just need to truncate the string with substr. Quote Link to comment Share on other sites More sharing options...
gergy008 Posted February 16, 2011 Author Share Posted February 16, 2011 You'd just need to truncate the string with substr. But that will cut some strings half way though a word, And won't give me the ... at the end, I seen it somwhere but I can't remember the name is it truncation or something? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted February 16, 2011 Share Posted February 16, 2011 I believe the word 'truncate' was in my previous response, yes, yes, I see it there! Quote Link to comment Share on other sites More sharing options...
gergy008 Posted February 16, 2011 Author Share Posted February 16, 2011 I believe the word 'truncate' was in my previous response, yes, yes, I see it there! My bad it was, And I found some code, I was looking for something like this: <?php function Truncate($string, $length, $stopanywhere=false) { //truncates a string to a certain char length, stopping on a word if not specified otherwise. if (strlen($string) > $length) { //limit hit! $string = substr($string,0,($length -3)); if ($stopanywhere) { //stop anywhere $string .= '...'; } else{ //stop on a word. $string = substr($string,0,strrpos($string,' ')).'...'; } } return $string; } ?> Quote Link to comment 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.