Get first n characters of a string

前端 未结 19 1168
萌比男神i
萌比男神i 2020-11-22 13:06

How can I get the first n characters of a string in PHP? What\'s the fastest way to trim a string to a specific number of characters, and append \'...\' if needed?

19条回答
  •  太阳男子
    2020-11-22 13:24

    This functionality has been built into PHP since version 4.0.6. See the docs.

    echo mb_strimwidth('Hello World', 0, 10, '...');
    
    // outputs Hello W...
    

    Note that the trimmarker (the ellipsis above) are included in the truncated length.

提交回复
热议问题