Is there a generic function that I can truncate variables that are pulled from CCK nodes, to a given word length?

I have a variable output using contemplate;
print $node->field_news_body[0]['view']

I know there's a setting in drupal for teasers to be truncated, i'm wondering if I can use the same thing.

Comments

stanbroughl’s picture

have a look at this thread http://drupal.org/node/78336 i don't know if it works in drupal 5.x but its worth a try - i came across this script a few months back and i love it!

luyendao’s picture

Thanks a lot Stan, i've read that posting a number of times in my efforts to understand CCK/views better.

It seems that using substr, only truncates to a char length, that means it could cut off mid-word, was that the case for you? That's a good start though, maybe i can modify it to do what i need.

Thanks for your reply!

stanbroughl’s picture

yes its a shame that it doesn't recognise the end of the word but not strictly necessary with the ...read more link at the end as it infers there's more and the user should click and go find it! :)

i can't remember where i found it originally on here so i can't take the credit for it!

good luck in your search

As If’s picture

Here's how to split at the end of a word...

<?php
if(strlen($body) > 90) {
  $chunk = substr($body,0,90);
  $lastspace = strrpos($chunk," ");
  $body = substr($chunk,0,$lastspace) . "...";
}
echo $body;
?>		

LVX
TF
-------------------------------------------
Interactive Worlds and Immersive Obsessions
http://www.asifproductions.com

-------------------------------------------
Interactive Worlds and Immersive Obsessions
http://www.asifproductions.com

luyendao’s picture

so simple! Thank you!

lias’s picture

for the code but where exactly do I put it? I've got this in my contemplate teaser:

<div class="field field-type-text field-field-your-review">
<?php print $teaser = substr($field_your_review[0]['value'], 0, 300) ?>
<span class="more-link"><?php print l('... more', 'node/'.$nid, array('title' => 'read more')); ?></span>
</div>

Thanks!

luyendao’s picture

<?php
if(strlen($field_your_review[0]['value']) > 90) {
  $chunk = substr($field_your_review[0]['value'],0,90);
  $lastspace = strrpos($chunk," ");
  $body = substr($chunk,0,$lastspace) . "...";
}
echo $body;
?> 

i think this should work, i just replaced $body with the CCK field you had.

lias’s picture

thanks luyendao for the code, I do appreciate it : )

I wanted to post the read more addition for anyone interested:

<?php
if(strlen($field_your_review[0]['value']) > 200) {
  $chunk = substr($field_your_review[0]['value'],0,200);
  $lastspace = strrpos($chunk," ");
  $body = substr($chunk,0,$lastspace) . "...";
}
echo $body;
?> <span class="more-link"><?php print l(' ..read more', 'node/'.$nid, array('title' => 'read more')); ?></span>
Rob T’s picture

Worked for me, too. Thanks for the snippet.

gav240z’s picture

I need some help here. I want to create a disk template only, not use contemplate for errrmmm cck templates.

I don't know what to call the teaser filename?

Thanks for the help.

argentin’s picture

Excelent! It works perfect for me... Thanks!