help to implement code ...

Discussion in 'PHP' started by perik, Jul 26, 2010.

  1. #1
    Hi!

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

    SC_BEGIN NEWSBODY2
    global $tp;
    $news_item = getcachedvars('current_news_item');
    $news_body = $tp -> toHTML($news_item['news_body']);
    return $news_body;
    SC_END
    PHP:
    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:

    I found a code that could achieve this:

    $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 . ';
    }
    PHP:
    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
     
    perik, Jul 26, 2010 IP
  2. Martin K

    Martin K Active Member

    Messages:
    262
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #2
    hope this helps
     
    Martin K, Jul 29, 2010 IP