Page 1 of 1

help to implement code ...

Posted: Mon Jul 26, 2010 8:05 pm
by perik78
Hi!

I got a shortcode (code that can be called elsewhere on the page) om my webpage that looks like this:

Code: Select all

SC_BEGIN NEWSBODY2
global $tp;
$news_item = getcachedvars('current_news_item');
$news_body = $tp -> toHTML($news_item['news_body']);
return $news_body;
SC_END
This calls a text from the mysql.

But I want to edit this code so if the text is more than say 3 words long it will return with three dots like this:
Word1 word2 word3 …
I found a code that could achieve this:

Code: Select all

$threshold_length = 3; // 3 words max
$phrase = 'word1 word2 word3 word4'; // populate this with the text you want to display
$abody = str_word_count($phrase,2);
if(count($abody) >= $threshold_length){ // gotta cut
  $tbody = array_keys($abody);
  echo '' . substr($phrase,0,$tbody[$threshold_length]) . '... ';
} else { // put the whole thing
  echo ' . $phrase . ';
}
I tried to implement this code into the shortcode above but cant succed. But Im not very good in coding.

Any help with solution would be very much apreciated.

So that the code just calls say the first three words in the mysql

Thanks!

/ P

Re: help to implement code ...

Posted: Mon Jul 26, 2010 10:27 pm
by requinix
Two things:
1. Use

Code: Select all

 instead of [php].
2. The shortening thing you have there isn't really that good. Find another code sample.

[syntax=php]global $tp;
$news_item = getcachedvars('current_news_item');
$news_body = $news_item['news_body'];

/* shorten $news_body here */

$news_body = $tp -> toHTML($news_body);
return $news_body;