PHP: properly truncating strings with “..”

前端 未结 6 1611
遇见更好的自我
遇见更好的自我 2021-02-07 08:59

Currently, the method I use to truncate strings is: echo substr($message, 0, 30).\"..\";

How do I show the dots only in the case that the string has been tr

6条回答
  •  失恋的感觉
    2021-02-07 09:28

    Just check the length of the original string to see if it needs to be truncated. If it is longer than 30, truncate the string and add the dots on the end:

    if (strlen($message) > 30) {
     echo substr($message, 0, 30)."..";
    } else {
     echo $message;
    }
    

提交回复
热议问题