GIDForums

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum > PHP Code Library
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 05-Jun-2003, 09:25
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough

[function] Show limited words of a string / text


PHP Code:

<?php

// NAME: limit_words()
// VERSION: 1.0
// AUTHOR: J de Silva
// DESCRIPTION: returns a specified number of words off a string
// 
// TYPE: function
// --------------------

function limit_words( $str, $num, $append_str='' )
{
  $words = preg_split( '/[\s]+/', $str, -1, PREG_SPLIT_OFFSET_CAPTURE );
  if( isset($words[$num][1]) )
  {
    $str = substr( $str, 0, $words[$num][1] ) . $append_str;
  }
  unset( $words, $num );
  return trim( $str );
}
// --------------------
// USE:

$longtext = 'This is a line of words. Some more words right here.';
// show just 6 words...
$shorttext = limit_words( $longtext, 6, '&nbsp;&nbsp;<a href="/?art=1&amp;show=all">more...</a>' );
echo $shorttext;

?>

 

Recent GIDBlogGoing to Iraq by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 12:46.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.