help to implement code ...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
perik78
Forum Newbie
Posts: 8
Joined: Mon Dec 28, 2009 5:20 pm

help to implement code ...

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: help to implement code ...

Post 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;
Post Reply