最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Truncate title to single line & container width

matteradmin4PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far