note 95878 added to function.preg-split
| From: | nesbert at gmail dot com | Date: | Wed, 27 Jan 2010 01:39:22 +0000 |
| Subject: | note 95878 added to function.preg-split | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-165348@lists.php.net to get a copy of this message | ||
Hope this is helpful to someone...
<?php
/**
* Split a string into groups of words with a line no longer than $max
* characters.
*
* @param string $string
* @param integer $max
* @return array
**/
function split_words($string, $max = 100)
{
$words = preg_split('/\s/', $string);
$lines = array();
$line = '';
foreach ($words as $word) {
$length = strlen($line . ' ' . $word);
if ($length <= $max) {
$line .= ' ' . $word;
} else if ($length > $max) {
if (!empty($line)) $lines[] = trim($line);
$line = $word;
} else {
$lines[] = trim($line) . ' ' . $word;
$line = '';
}
}
$lines[] = $word;
return $lines;
}
?>
----
Server IP: 64.71.164.2
Probable Submitter: 208.95.108.130
----
Manual Page -- http://www.php.net/manual/en/function.preg-split.php
Edit -- https://master.php.net/note/edit/95878
Del: integrated -- https://master.php.net/note/delete/95878/integrated
Del: useless -- https://master.php.net/note/delete/95878/useless
Del: bad code -- https://master.php.net/note/delete/95878/bad+code
Del: spam -- https://master.php.net/note/delete/95878/spam
Del: non-english -- https://master.php.net/note/delete/95878/non-english
Del: in docs -- https://master.php.net/note/delete/95878/in+docs
Del: other reasons-- https://master.php.net/note/delete/95878
Reject -- https://master.php.net/note/reject/95878
Search -- https://master.php.net/manage/user-notes.php