note 71163 added to function.preg-split

From: Date: Tue, 14 Nov 2006 13:56:34 +0000
Subject: note 71163 added to function.preg-split
Groups: php.notes 
Request: Send a blank email to php-notes+get-119718@lists.php.net to get a copy of this message
This script splits a text into portions of a defined max. size, which will never be exceeded, and doesnt cut words. (Per portion it adds as many words as possible without exceeding the char-limit) the only exception where a portion would be bigger than the limit, is when there's a word thats longer than the max_size, but you could quite easily change the script so it regards this. <? $str= 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'; $max_size = 50; $words = preg_split("/[\040]+/", $str, -1); $r=0; for($i=0; $i < count($words); $i++) { if (strlen($line[$r] . $words[$i] . ' ') < $max_size) $line[$r] .= $words[$i] . ' '; else { $r++; $line[$r] .= $words[$i] . ' '; } } print_r ($line); ?> Result: Array ( [0] => Lorem ipsum dolor sit amet, consectetur [1] => adipisicing elit, sed do eiusmod tempor [2] => incididunt ut labore et dolore magna aliqua. Ut [3] => enim ad minim veniam, quis nostrud exercitation [4] => ullamco laboris nisi ut aliquip ex ea commodo [5] => consequat. Duis aute irure dolor in [6] => reprehenderit in voluptate velit esse cillum [7] => dolore eu fugiat nulla pariatur. Excepteur sint [8] => occaecat cupidatat non proident, sunt in culpa [9] => qui officia deserunt mollit anim id est laborum. ) ---- Server IP: 194.153.94.10 Probable Submitter: 217.162.98.228 ---- Manual Page -- http://www.php.net/manual/en/function.preg-split.php Edit -- https://master.php.net/note/edit/71163 Del: integrated -- https://master.php.net/note/delete/71163/integrated Del: useless -- https://master.php.net/note/delete/71163/useless Del: bad code -- https://master.php.net/note/delete/71163/bad+code Del: spam -- https://master.php.net/note/delete/71163/spam Del: non-english -- https://master.php.net/note/delete/71163/non-english Del: in docs -- https://master.php.net/note/delete/71163/in+docs Del: other reasons-- https://master.php.net/note/delete/71163 Reject -- https://master.php.net/note/reject/71163 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#119718) next »