How to get first x chars from a string, without cutting off the last word?

后端 未结 13 1371
再見小時候
再見小時候 2020-11-28 10:24

I have the following string in a variable.

Stack Overflow is as frictionless and painless to use as we could make it.

I want to fetch first 28 characte

13条回答
  •  余生分开走
    2020-11-28 10:54

    I would use a string tokenizer to split the string into words much like this:

    $string = "Stack Overflow is as frictionless and painless to use as we could make it.";
    $tokenized_string = strtok($string, " ");
    

    Then you can pull out the individual words any way you want.


    Edit: Greg has a much better and more elegant way of doing what you want. I would go with his wordwrap() solution.

提交回复
热议问题