Limiting Text Volume

Permalink
I am quite new to PHP..
Is it possible to set the limit to Text Volume in a block, using PHP?

 
griebel replied on at Permalink Reply
griebel
If you need to truncate a text, try this PHP function.

function truncateString($str, $max, $rep = '...') {
  if(strlen($str) > $max) {
    $leave = $max - strlen($rep);
    return substr_replace($str, $rep, $leave);
  } else {
    return $str;
  }
}
print truncateString("Here is my faux really long string", 10);


10 is the number of characters (your limit)
Tony replied on at Permalink Reply
Tony
Concrete5 also has a bunch of "helpers" for stuff like this. Just browse through c5root/concrete/helpers/ and explore what's available.

Here's c5's built in function for shortening text:

$numberOfChars=255;
$textHelper = Loader::helper("text");  
echo $textHelper->shortText( $myLongText, $numberOfChars );
kenichi replied on at Permalink Reply
griebel,

It works in the fixed contents but I guess I need to arrange this code when appling to the editable contents like the following part in the theme.

<?
$a = new Area('photo');
$a->display($c);
?>


Thanks anyway!


Tony,

Where should I put this?

Actually, I put it into the abovementioned code as it is but nothing's happened..

Could you give me Further advise, please?
Tony replied on at Permalink Reply
Tony
it has pretty much exactly the same interface as griebel's function (except it is a method of an object instead of a function). As long as you're using concrete 5 it should work.
kenichi replied on at Permalink Reply
I'm not familiar with PHP at all...

When I use the code you told me, I put it in the new Area code like the following. Is this correct?

<?   
$a = new Area('article');  
$a->display($c);  
$numberOfChars=255;  
$textHelper = Loader::helper("text");    
echo $textHelper->shortText( $myLongText, $numberOfChars );  
?>
Tony replied on at Permalink Reply 1 Attachment
Tony
ok. The $myLongText is a variable that I was assuming you'd swap out with a variable holding the text you wanted to truncate.

For what you're doing, I'd really recommend running the shortText function on an actual block rather than the area, simply because it doesn't make sense to truncate the markup of most other blocks since it will render them useless. You'd do this by creating a custom template for a block, which in your case will probably be the content block.

i'm attaching a file to this post. unzip it and copy the file to yourConcreteRoot/blocks/content/templates/

Then after adding a content block to that area, click it, select the "set custom template", and select this new shortText view.

(Note that I'm also running strip_tags in that template, cause otherwise it might chop the text off in the middle of an html tag)
kenichi replied on at Permalink Reply
It works well!

I'm wondering that if it's possible to set a custom template, maintaining css code defined in the theme.