Few Letters Preview of Text Field from Database Table

Discussion in 'PHP' started by Odublar, Jan 22, 2006.

  1. #1
    How do I call only the first few letters, like say 20 of the first letters from a text field of a table.

    If the ItemID = $a1[ItemID], then how do I select just 20 Letters from the Field Entry $a1[ItemText]? Any help would be appreciated.

    Thanks in Advance.

    Steven
     
    Odublar, Jan 22, 2006 IP
  2. GetWebHost

    GetWebHost Active Member

    Messages:
    735
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    78
    #2
    Use substr() -> http://www.php.net/substr
     
    GetWebHost, Jan 22, 2006 IP
  3. Odublar

    Odublar Guest

    Messages:
    78
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks bro, will look into it.
     
    Odublar, Jan 22, 2006 IP
  4. GetWebHost

    GetWebHost Active Member

    Messages:
    735
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    78
    #4
    Welcome :)
     
    GetWebHost, Jan 22, 2006 IP
    Odublar likes this.
  5. dave487

    dave487 Peon

    Messages:
    701
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #5
    
    $str=$a1[ItemText];
    $length=20;
    $trailing='...';
          // take off chars for the trailing
          $length-=strlen($trailing);
          if (strlen($str) > $length) 
          {
             // string exceeded length, truncate and add trailing dots
             $string=substr($str,0,$length).$trailing;
          } 
          else 
          { 
             // string was already short enough, return the string
             $string = $str; 
          }
    
    PHP:
     
    dave487, Jan 23, 2006 IP
    Odublar likes this.
  6. GetWebHost

    GetWebHost Active Member

    Messages:
    735
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    78
    #6
    Here's what I used on the Bulletin hack I modified:
    function truncateEntry($string, $limit, $break=".", $pad="...") 
    { 
        # return with no change if string is shorter than $limit 
        if(strlen($string) <= $limit) return; 
        # is $break present between $limit and the end of the string? 
        $breakpoint = strpos($string, $break, $limit); 
        if($breakpoint < strlen($string) - 1) 
        { 
            # truncate string and pad 
            $string = substr($string, 0, $breakpoint) . $pad; 
        } 
        return $string;
    } 
    PHP:
     
    GetWebHost, Jan 23, 2006 IP