Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP - Text Length

Options
  • 31-05-2007 10:11pm
    #1
    Registered Users Posts: 1,987 ✭✭✭


    I was wondering can anyone help me, say i query a DB and get a string back with 200 characters, but i only want to display the first 100 characters, is this possible in php?? Tried everything cant figure it out!??


Comments

  • Registered Users Posts: 6,511 ✭✭✭daymobrew


    Use substr(). If you want to add the '...' text to indicate that the string is truncated, you could use wordwrap() to split it at 100 chars (on a word boundary) and then append '...'.


  • Registered Users Posts: 1,987 ✭✭✭Ziycon


    [PHP]wordwrap(substr($row, 0, 15), 15, "...")[/PHP]
    Its displaying 15 characters but not the '...' any ideas?


  • Closed Accounts Posts: 593 ✭✭✭McSandwich


    Ziycon wrote:
    I was wondering can anyone help me, say i query a DB and get a string back with 200 characters, but i only want to display the first 100 characters, is this possible in php?? Tried everything cant figure it out!??

    See: http://ie2.php.net/substr

    edit: Sorry, jumped in too quick there - didn't see this posted above...


  • Closed Accounts Posts: 8,478 ✭✭✭GoneShootin


    echo substr($row['topic_name'], 0, 15)."...";
    

    should work


  • Registered Users Posts: 94 ✭✭Kudos


    I don't know why wordwrap was suggested as it has nothing to do with this, it just inserts <br /> periodically.


  • Advertisement
  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    You can also chop off the first 100 characters before retrieving it from the database using the SUBSTR() function (in MySQL).

    The problem is that doesn't allow for elegant solutions.

    Just to expand on Goneshootin's piece of code - you want to chop it down to 15 characters, and displaying an ellipses to indicate that there is more text. But what if the text is 15 characters long, or less?
    echo (strlen($row['topic_name']) <= 15)? $row['topic_name'] :  substr($row['topic_name'], 0, 15)."...";
    


  • Moderators, Politics Moderators Posts: 39,920 Mod ✭✭✭✭Seth Brundle


    Will the output include HTML. This could make some outputs seem very small. There may also be a problem of unclosed tags.


  • Registered Users Posts: 6,511 ✭✭✭daymobrew


    Ziycon wrote:
    [PHP]wordwrap(substr($row, 0, 15), 15, "...")[/PHP]
    Its displaying 15 characters but not the '...' any ideas?
    It doesn't work because the string fed to wordwrap is only 15 chars and wordwrap doesn't need to split it.
    I wrote a function that will append the dots if the string is more than specified characters long.
    It gets wordwrap to break the string, adding a special delimiter (a char that will not be in the string or has a very low probability of being in the string). Then it breaks that new string into an array, breaking on that special delimiter. It then appends the dots to the first element of that string.
    [PHP]<?php
    function AddDots( $text )
    {
    define( 'STR_BREAK_LEN', 15 );
    define( 'BREAK_DELIMITER', '|' );

    if ( strlen( $text ) > STR_BREAK_LEN ) {
    // Call wordwrap with a special delimiter and then create
    // a list based on that delimiter.
    $list = explode( BREAK_DELIMITER, wordwrap( $text, STR_BREAK_LEN, BREAK_DELIMITER ) );
    // Append the ellipses.
    return $list[0]. '...';
    }
    else
    return $text;
    }

    $text = "The quick brown fox jumped over the lazy dog.";
    echo AddDots( $text ), "\n";
    ?>[/PHP]


  • Registered Users Posts: 2,934 ✭✭✭egan007


    Ziycon wrote:
    I was wondering can anyone help me, say i query a DB and get a string back with 200 characters, but i only want to display the first 100 characters, is this possible in php?? Tried everything cant figure it out!??

    FYI, all questions you will ever have about PHP will be answered by this search
    http://www.php.net


  • Registered Users Posts: 1,987 ✭✭✭Ziycon


    So does this code only add the dots if the string returned is greater then 15 characters?
    egan007 wrote:
    FYI, all questions you will ever have about PHP will be answered by this search
    http://www.php.net
    Use it all the time but couldn't find the answer to this question.


  • Advertisement
  • Registered Users Posts: 6,511 ✭✭✭daymobrew


    Kudos wrote:
    I don't know why wordwrap was suggested as it has nothing to do with this, it just inserts <br /> periodically.
    I suggested wordwrap because it breaks on word boundaries. If OP doesn't mind breaking mid-word, then seamus' solution is nice and simple.
    WRT inserting 'br' code, the default wordwrap behaviour is to add \n and break on 75 chars.


  • Registered Users Posts: 6,511 ✭✭✭daymobrew


    Ziycon wrote:
    So does this code only add the dots if the string returned is greater then 15 characters?
    Yes.


  • Registered Users Posts: 1,987 ✭✭✭Ziycon


    daymobrew wrote:
    Yes.
    Cool, i'll try it when i get home, thanks to everyone for their input!


  • Registered Users Posts: 94 ✭✭Kudos


    daymobrew wrote:
    I suggested wordwrap because it breaks on word boundaries. If OP doesn't mind breaking mid-word, then seamus' solution is nice and simple.
    WRT inserting 'br' code, the default wordwrap behaviour is to add \n and break on 75 chars.

    Ah yes, makes perfect sense now.


  • Registered Users Posts: 1,987 ✭✭✭Ziycon


    Just got a chance to try the code there, thanks daymobrew, much appreciated!:D


Advertisement