Home > Article > Backend Development > How to Truncate Strings While Preserving Word Integrity in PHP?
Preserving Word Integrity When Truncating Strings
In a previous inquiry, you expressed a need to shorten strings to 100 characters while maintaining the integrity of words. The initial approach using substr() blindly cut off at the 100th character.
To achieve your desired result, consider the following PHP code:
<code class="php">$pos = strpos($content, ' ', 200); substr($content, 0, $pos);</code>
This code:
The above is the detailed content of How to Truncate Strings While Preserving Word Integrity in PHP?. For more information, please follow other related articles on the PHP Chinese website!