trim a long text to shorter

Discussion in 'PHP' started by dracula51, Oct 21, 2009.

  1. #1
    this is the most easiest way-

    <?
    $var = "The quick brown fox jumps over the lazy dog";
    
    echo substr($var,0,13);
    ?>
    PHP:
    this will show the first 13 charecter of the text (including Space)

    result is
    The quick bro
    Code (markup):
    but i want
    The quick bro...
    Code (markup):
    i mean adding some ... after trimming (& no ... if not trimmed)

    help me plz
     
    dracula51, Oct 21, 2009 IP
  2. jpinheiro

    jpinheiro Peon

    Messages:
    1,211
    Likes Received:
    15
    Best Answers:
    1
    Trophy Points:
    0
    #2
    this should do it :)

    
    <?php
    
    $var = "The quick brown fox jumps over the lazy dog";
    $len = strlen($var);
    
    if($len > 13) {
    
    $var = substr($var,0,13);
    
    echo "$var ...";
    
    } else {
    
    echo "$var";
    
    }
    
    ?>
    
    PHP:
    -John
     
    jpinheiro, Oct 21, 2009 IP
  3. dracula51

    dracula51 Peon

    Messages:
    146
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    tnx mate...working nice :)
     
    dracula51, Oct 21, 2009 IP
  4. jpinheiro

    jpinheiro Peon

    Messages:
    1,211
    Likes Received:
    15
    Best Answers:
    1
    Trophy Points:
    0
    #4
    not a problem at all :)

    any other questions just PM me :)

    -John
     
    jpinheiro, Oct 21, 2009 IP
  5. heavydev

    heavydev Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    
    <?php
     $var = 'The quick brown fox jumps over the lazy dog';
     $abbrv = strlen($var)>13 ? substr($var,0,13).'...' : $var;
     echo $abbrv;   
    ?>
    
    PHP:
    If you wanted a condensed solution...
     
    heavydev, Oct 23, 2009 IP