note 95880 added to function.preg-split

From: Date: Wed, 27 Jan 2010 01:57:54 +0000
Subject: note 95880 added to function.preg-split
Groups: php.notes 
Request: Send a blank email to php-notes+get-165350@lists.php.net to get a copy of this message
Hope this helps 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 = 1) { $words = preg_split('/\s/', $string); $lines = array(); $line = ''; foreach ($words as $k => $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[] = !count($lines) ? trim($line) : $word; return $lines; } ?> Please disregard previous note. ---- 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/95880 Del: integrated -- https://master.php.net/note/delete/95880/integrated Del: useless -- https://master.php.net/note/delete/95880/useless Del: bad code -- https://master.php.net/note/delete/95880/bad+code Del: spam -- https://master.php.net/note/delete/95880/spam Del: non-english -- https://master.php.net/note/delete/95880/non-english Del: in docs -- https://master.php.net/note/delete/95880/in+docs Del: other reasons-- https://master.php.net/note/delete/95880 Reject -- https://master.php.net/note/reject/95880 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#165350) next »