admin管理员组

文章数量:1434956

Is there a way to truncate a title to the container width? I'm able to do truncate a long title to one line using the code below, but that doesn't really work for mobile.

<h3>
    <?php
        $thetitle = $post->post_title;
        $getlength = strlen($thetitle);
        $thelength = 33;
        echo substr($thetitle, 0, $thelength);
        if ($getlength > $thelength) echo "...";
    ?>
</h3>

Is there a way to truncate a title to the container width? I'm able to do truncate a long title to one line using the code below, but that doesn't really work for mobile.

<h3>
    <?php
        $thetitle = $post->post_title;
        $getlength = strlen($thetitle);
        $thelength = 33;
        echo substr($thetitle, 0, $thelength);
        if ($getlength > $thelength) echo "...";
    ?>
</h3>
Share Improve this question asked Mar 1, 2014 at 2:15 vytflavytfla 1831 silver badge7 bronze badges 1
  • Not with PHP. PHP executes on teh server and has no idea how the page gets rendered. – s_ha_dum Commented Mar 1, 2014 at 3:54
Add a comment  | 

1 Answer 1

Reset to default 1

how about using CSS3's "text-overflow:ellipsis;" ?

doing so will make sure your title stays in a single line, trimming whatever crossed the container's width limit.

本文标签: phpTruncate title to single line amp container width