note 73233 added to function.substr

From: Date: Wed, 14 Feb 2007 10:20:02 +0000
Subject: note 73233 added to function.substr
Groups: php.notes 
Request: Send a blank email to php-notes+get-123358@lists.php.net to get a copy of this message
/* 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")); */ ---- Server IP: 130.206.1.5 Probable Submitter: 83.165.222.51 ---- Manual Page -- http://www.php.net/manual/en/function.substr.php Edit -- https://master.php.net/note/edit/73233 Del: integrated -- https://master.php.net/note/delete/73233/integrated Del: useless -- https://master.php.net/note/delete/73233/useless Del: bad code -- https://master.php.net/note/delete/73233/bad+code Del: spam -- https://master.php.net/note/delete/73233/spam Del: non-english -- https://master.php.net/note/delete/73233/non-english Del: in docs -- https://master.php.net/note/delete/73233/in+docs Del: other reasons-- https://master.php.net/note/delete/73233 Reject -- https://master.php.net/note/reject/73233 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#123358) next »