Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

controlling text length with PHP - Please help!

Status
Not open for further replies.

WeeWeb

Technical User
Joined
Mar 27, 2004
Messages
3
Location
GB
Hi all
I have a news database and rather than show the full stories right away I want to have a small amount of text for each story.

I want to be able to just show the first 120 characters of the news stories so that people get an idea of what the full stories are actually about.

Using PHP how do I do limit the amount of characters that show?

All help would be well appreciated
Rab
 
$STRIING="A WHOLE LOT OF VERY VERY NICE COLOURFUL TEXT THAT YOU DONT WANT EVERYONE TO SEE ALL AT ONCE AND DAYM caps lock ..lol ok whatever";

$preview=substr($STRING, 0, 120); // string , start, end.



______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Fantastic stuff KarveR

Is there any way of cutting it of at a certain number of words rather than characters and also how would I add 'dots' to show that it is continuing text like this . . .

Thanks for your help
Rab
 
Maybe the function strtok at php.net can make a space a token to break at.

Code:
print ("$preview ...");

- - picklefish - -
Why is everyone in this forum responding to me as picklefish?
 
Not quite sure what is meant by this - I just need the words to cut-off , say after 12 words and have three dots on the end like this . . .

Any ideas?

Cheers
Rab
 
Try this
Code:
$str_to_print = substr($longstr,0,12) ;
$str_to_print .= "..." ;
echo $str_to_print ;


--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
the code example I offered was only to add the ellipse after the substring that KarveR offered (building on his defined variable). spookie's code will still cut off at the 12th character, not 12th word.

You might try turning your text string into an array by breaking elements at spaces. Then simply print out the first 12 elements in that array.

- - picklefish - -
Why is everyone in this forum responding to me as picklefish?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top