note 114474 added to function.preg-split

From: Date: Mon, 24 Feb 2014 17:25:19 +0000
Subject: note 114474 added to function.preg-split
Groups: php.notes 
Request: Send a blank email to php-notes+get-198632@lists.php.net to get a copy of this message
This is a function to truncate a string of text while preserving the whitespace (for instance, getting an excerpt from an article while maintaining newlines). It will not jive well with HTML, of course. <?php /** * Truncates a string of text by word count * @param string $text The text to truncate * @param int $max_words The maximum number of words * @return string The truncated text */ function limit_words ($text, $max_words) { $split = preg_split('/(\s+)/', $text, -1, PREG_SPLIT_DELIM_CAPTURE); $truncated = ''; for ($i = 0; $i < min(count($split), $max_words*2); $i += 2) { $truncated .= $split[$i].$split[$i+1]; } return trim($truncated); } ?> ---- Server IP: 64.71.164.2 Probable Submitter: 68.14.223.28 ---- Manual Page -- http://php.net/manual/en/function.preg-split.php Edit -- https://master.php.net/note/edit/114474 Del: integrated -- https://master.php.net/note/delete/114474/integrated Del: useless -- https://master.php.net/note/delete/114474/useless Del: bad code -- https://master.php.net/note/delete/114474/bad+code Del: spam -- https://master.php.net/note/delete/114474/spam Del: non-english -- https://master.php.net/note/delete/114474/non-english Del: in docs -- https://master.php.net/note/delete/114474/in+docs Del: other reasons-- https://master.php.net/note/delete/114474 Reject -- https://master.php.net/note/reject/114474 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#198632) next »