Results 1 to 3 of 3

Thread: Display a certain amount of characters from a string?

  1. #1
    Join Date
    Feb 2009
    Location
    The land "Down Under"
    Posts
    10
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Display a certain amount of characters from a string?

    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

  2. #2
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    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 Code:
    <?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($thread050) . "...";

    // 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!
    Last edited by Schmoopy; 02-03-2009 at 10:37 PM.

  3. #3
    Join Date
    Feb 2009
    Location
    The land "Down Under"
    Posts
    10
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default

    Awesome stuff mate, thanks for that; i will give it a try shortly!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •