How to capture complete words using substr() in PHP, limit by word?
Solution 1
If you just count the words the resulting sting could still be very long as a single "word" might have 30 characters or more. I would suggest instead truncating the text to 100 characters, except if this causes a word to be truncated then you should also remove the truncated part of the word. This is covered by this related question:
How to Truncate a string in PHP to the word closest to a certain number of characters?
Using wordwrap
$your_desired_width = 100;
if (strlen($string) > $your_desired_width)
{
$string = wordwrap($string, 100);
$i = strpos($string, "\n");
if ($i) {
$string = substr($string, 0, $i);
}
}
This is a modified versions of the answer here. if the input text could be very long you can add this line before the call to wordwrap to avoid wordwrap having to parse the entire text:
$string = substr($string, 0, 101);
Using a regular expression (Source)
$string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, 100));
Solution 2
$a = explode('|', wordwrap($string, 100, '|');
print $a[0];
Solution 3
Try a single line code
$string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length));
Solution 4
// Trim very long text to 120 characters. Add an ellipsis if the text is trimmed.
if(strlen($very_long_text) > 120) {
$matches = array();
preg_match("/^(.{1,120})[\s]/i", $very_long_text, $matches);
$trimmed_text = $matches[0]. '...';
}
nectar
Updated on March 05, 2020Comments
-
nectar about 4 years
When I use
substr($string,0,100)
, it gives first 100 characters. Sometimes it left the last word incomplete. That looks odd. Can I do limit by word rather than char? -
Matthew over 13 yearsI would avoid using the wordwrap function as it will unnecessarily parse the entire string. The regular expression is the better of the two, IMO.
-
Mark Byers over 13 years@konforce: I agree the regular expression solution is simple and flexible but some people aren't comfortable with regular expressions. I have proposed a new version of the wordwrap solution. Is it OK now?
-
Kim Stacks about 10 yearsthe regular expression will accidentally remove the last word. I have added code to include a check. See the edit.
-
Peter about 8 yearsThis fails if the character | accidentally occurs in $string. Better use chr(0) or something like it that will never occur in the $string.