#14236 (Feature suggestion) Improved "crop" function.
Description
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($options0);
$afterstring=trim($options1);
$crop2space = trim($options2);
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=[|]
20 = TEXT
20.value = She got a great assistent
20.crop = 19|..| 1
20.wrap = Space crop at 19 chars=[|]<br>
}
— cut —
Output of test example:
Standard crop at 19 chars=[She got a great ass..]
Space crop at 19 chars=[She got a great..]
—-
Peter Klein / Umloud ApS
Not really a bug, but Peter Niederlag suggested that I post it here anyway..
(issue imported from #M213)
Reviews 0
No change resolves or names this issue in its commit message, and no comment links one.