Log in

View Full Version : Display a certain amount of characters from a string?



Stinger
02-03-2009, 10:04 PM
Hi there

I only recently found this website but wow what a great resource it is!

I am currently working on a new project of my own, a website and forum for the model of car I own and love; and on the home page (index.php) I have a piece of code which calls a file that then displays a number of the latest threads from my forums.

This all works fine and it displays the 5 newest threads nicely. The problem I have is some of the thread topics it displays on the home page are too long and it busts the table or goes to a second line which I don't want to happen.

Is there a way I can have it only display a certain amount of characters instead of the entire thread topic. Something like this; "This is the name of my new threa...." and stop when it reaches the limit.

If you want to see the code im using please let me know.


Thanks

Ryan

Schmoopy
02-03-2009, 10:18 PM
Hi, this is where the substr() function would come in useful : http://uk.php.net/manual/en/function.substr.php

You can use it to take a string and then only return part of it, like so:



<?php
$thread = "I am a thread, I am really long and need to be shortened";

// Say you want to only return 50 characters you would do the following

$val = substr($thread, 0, 50) . "...";

// Translated it would be - Start at the beginning of $thread and move along 50 characters, and discard the rest


echo $val;
// This returns : I am a thread, I am really long and need to be sho...




Hope that helps you.

Edit: Welcome to the forums! :D

Stinger
02-03-2009, 10:45 PM
Awesome stuff mate, thanks for that; i will give it a try shortly!:D