How do I only display part of a string using css

前端 未结 7 1928
梦谈多话
梦谈多话 2021-02-05 19:01

I want to be able to display a string of characters up to 10 characters. If the string goes over 10 characters, I\'d like to append \'...\' to the end.

For example, if

7条回答
  •  花落未央
    2021-02-05 19:59

    Without using any serverside script or javascript you can not do this.

    Use below function.

    function LimitCharacter($data,$limit = 20)
    {
        if (strlen($data) > $limit)
        {
            $data = substr($data, 0, strrpos(substr($data, 0, $limit), ' ')) . '...';
            return $data;
        }
        else
        {
            return $data;
        }
    }
    

    call it as LimitCharacter($yourString,5);

    Javascript

    var str = 'Some very long string';
    if(str.length > 10) str = str.substring(0,10)+"...";
    

    CSS

    .limtiCharClass {
        -o-text-overflow: ellipsis;   /* Opera */
        text-overflow:    ellipsis;   /* IE, Safari (WebKit) */
        overflow:hidden;              /* don't show excess chars */
        white-space:nowrap;           /* force single line */
        width: 300px;                 /* fixed width */
    }
    

提交回复
热议问题