[Typo3-dev] (Feature suggestion) Improved "crop" function.

Peter Klein peter at umloud.dk
Mon Jul 12 11:38:44 CEST 2004


Hi list. here's a small update suggestion for the TS crop function. It adds
a 3rd parameter to the crop function. (100% compatible with the existing
crop function)

When the 3rd parameter (boolean) is set, then the crop function will crop at
the nearest space.

-- cut --
/**
  * Implements the stdWrap property "crop" which is a modified "substr"
function allowing to limit a string lenght to a certain number of chars
(from either start or end of string) and having a pre/postfix applied if the
string really was cropped.
  *
  * @param string  The string to perform the operation on
  * @param string  The parameters splitted by "|": First parameter is the
max number of chars of the string. Negative value means cropping from end of
string. Second parameter is the pre/postfix string to apply if cropping
occurs. Third parameter is a boolean value. If set then crop will be applied
at nearest space.
  * @return string  The processed input value.
  * @access private
  * @see stdWrap()
  */
 function crop($content,$options) {
  $options = explode('|',$options);
  $chars=intval($options[0]);
  $afterstring=trim($options[1]);
  $crop2space = trim($options[2]);
  if ($chars) {
   if (strlen($content)>abs($chars)) {
    if ($chars<0) {
     $content= substr($content,$chars);
     $trunc_at = strpos($content, ' ');
     $content = ($trunc_at&&$crop2space)?$afterstring.substr($content,
$trunc_at):$afterstring.$content;
    } else {
     $content = substr($content, 0, $chars);
     $trunc_at = strrpos($content, ' ');
     $content = ($trunc_at&&$crop2space)?substr($content, 0,
$trunc_at).$afterstring:$content.$afterstring;
    }
   }
  }
  return $content;
 }

-- cut --



Try out this example with the extended crop function..
-- cut --
temp.test = COA
temp.test {
  10 = TEXT
  10.value = She got a great assistent
  10.crop = 19|..
  10.wrap = Standard crop at 19 chars=[|]<br>

  20 = TEXT
  20.value = She got a great assistent
  20.crop = 19|..| 1
  20.wrap = Space crop atl 19 chars=[|]<br>
}
-- cut --

Output of test example:

Standard crop til 19 chars=[She got a great ass..]
Space crop til 19 chars=[She got a great..]


---
Peter Klein / Umloud ApS






More information about the TYPO3-dev mailing list