Home >Backend Development >PHP Tutorial >How to Make PHP's substr Function Break Strings at Word Boundaries?
Ensuring PHP substr Breaks Strings at Word Boundaries
When truncating strings using the substr function, it's important to ensure that the cutoff occurs at the end of a word, not in the middle of one. This is crucial for maintaining readability and conveying the intended meaning in text output.
To achieve this, you can utilize the following code:
substr($body, 0, strpos($body, ' ', 260))
In this snippet:
The above is the detailed content of How to Make PHP's substr Function Break Strings at Word Boundaries?. For more information, please follow other related articles on the PHP Chinese website!