dadamssg Posted April 27, 2009 Share Posted April 27, 2009 i want to only pull up a limited amount of words not characters and display a Read More button...im assuming i use substr but don't know how to make it pull up whole words...i don't want to cut off a word, thanks Quote Link to comment https://forums.phpfreaks.com/topic/155874-solved-read-more-part/ Share on other sites More sharing options...
premiso Posted April 27, 2009 Share Posted April 27, 2009 Moving to PHP Help, please take note to which forum you are posting in (this is the regex). Reading at substr in the user comments came across: http://us3.php.net/manual/en/function.substr.php#73233 <?php /* An advanced substr but without breaking words in the middle. Comes in 3 flavours, one gets up to length chars as a maximum, the other with length chars as a minimum up to the next word, and the other considers removing final dots, commas and etcteteras for the sake of beauty (hahaha). This functions were posted by me some years ago, in the middle of the ages I had to use them in some corporations incorporated, with the luck to find them in some php not up to date mirrors. These mirrors are rarely being more not up to date till the end of the world... Well, may be am I the only person that finds usef not t bre word in th middl? Than! (ks) This is the calling syntax: snippet(phrase,[max length],[phrase tail]) snippetgreedy(phrase,[max length before next space],[phrase tail]) */ function snippet($text,$length=64,$tail="...") { $text = trim($text); $txtl = strlen($text); if($txtl > $length) { for($i=1;$text[$length-$i]!=" ";$i++) { if($i == $length) { return substr($text,0,$length) . $tail; } } $text = substr($text,0,$length-$i+1) . $tail; } return $text; } // It behaves greedy, gets length characters ore goes for more function snippetgreedy($text,$length=64,$tail="...") { $text = trim($text); if(strlen($text) > $length) { for($i=0;$text[$length+$i]!=" ";$i++) { if(!$text[$length+$i]) { return $text; } } $text = substr($text,0,$length+$i) . $tail; } return $text; } // The same as the snippet but removing latest low punctuation chars, // if they exist (dots and commas). It performs a later suffixal trim of spaces function snippetwop($text,$length=64,$tail="...") { $text = trim($text); $txtl = strlen($text); if($txtl > $length) { for($i=1;$text[$length-$i]!=" ";$i++) { if($i == $length) { return substr($text,0,$length) . $tail; } } for(;$text[$length-$i]=="," || $text[$length-$i]=="." || $text[$length-$i]==" ";$i++) {;} $text = substr($text,0,$length-$i+1) . $tail; } return $text; } /* echo(snippet("this is not too long to run on the column on the left, perhaps, or perhaps yes, no idea") . "<br>"); echo(snippetwop("this is not too long to run on the column on the left, perhaps, or perhaps yes, no idea") . "<br>"); echo(snippetgreedy("this is not too long to run on the column on the left, perhaps, or perhaps yes, no idea")); */ ?> Quote Link to comment https://forums.phpfreaks.com/topic/155874-solved-read-more-part/#findComment-820465 Share on other sites More sharing options...
dadamssg Posted April 27, 2009 Author Share Posted April 27, 2009 thank you Quote Link to comment https://forums.phpfreaks.com/topic/155874-solved-read-more-part/#findComment-820473 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.