How to trim this line of code

Discussion in 'PHP' started by philpbvc3232, Mar 18, 2011.

  1. #1
    This php code displays a name from the database but sometimes the names are too long. How do i trim the name to only display 20 characters?

    $itemhtml = str_replace( '{name}', $item->link_name, $itemhtml );
     
    philpbvc3232, Mar 18, 2011 IP
  2. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #2
    
    $itemhtml = str_replace( '{name}', $item->link_name, $itemhtml ); 
    
    // Now we shorten $itemhtml to 20 characters
    $itemhtml = substr("$itemhtml", 0, 20);
    
    PHP:
     
    MyVodaFone, Mar 18, 2011 IP
  3. rockowtham

    rockowtham Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi..,

    Substr() function in php will give you a solution for your problem.

    Thanks
     
    rockowtham, Mar 21, 2011 IP
  4. SeoKungFu

    SeoKungFu Active Member

    Messages:
    206
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    55
    #4
    I bet it'll run as one liner as well :)
     
    SeoKungFu, Mar 22, 2011 IP
  5. vediovis

    vediovis Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    $itemhtml = substr("$itemhtml", 0, 20);
     
    vediovis, Mar 22, 2011 IP