note 83488 added to function.wordwrap

From: Date: Wed, 28 May 2008 11:56:33 +0000
Subject: note 83488 added to function.wordwrap
Groups: php.notes 
Request: Send a blank email to php-notes+get-139912@lists.php.net to get a copy of this message
I needed an UTF8/Unicode compatible wordwrap with the same features. As i searched the internet several times, and havent found anything. I created one my self. <?php public static function utf8Wordwrap($str, $width=75, $break="\n", $cut=false) { $splitedArray = array(); $lines = explode("\n", $str); foreach ($lines as $line) { $lineLength = strlen($line); if ($lineLength > $width) { $words = explode("\040", $line); $lineByWords = ''; $addNewLine = true; foreach ($words as $word) { $lineByWordsLength = strlen($lineByWords); $tmpLine = $lineByWords.((strlen($lineByWords) !== 0) ? ' ' : '').$word; $tmplineByWordsLength = strlen($tmpLine); if ($tmplineByWordsLength > $width && $lineByWordsLength <= $width && $lineByWordsLength !== 0) { $splitedArray[] = $lineByWords; $lineByWords = ''; } $newLineByWords = $lineByWords.((strlen($lineByWords) !== 0) ? ' ' : '').$word; $newLineByWordsLength = strlen($newLineByWords); if ($cut && $newLineByWordsLength > $width) { for ($i = 0; $i < $newLineByWordsLength; $i = $i + $width) { $splitedArray[] = mb_substr($newLineByWords, $i, $width); } $addNewLine = false; } else { $lineByWords = $newLineByWords; } } if ($addNewLine) { $splitedArray[] = $lineByWords; } } else { $splitedArray[] = $line; } } return implode($break, $splitedArray); } ?> Hope someone else can use this also. (Also all improvements are welcome) ---- Server IP: 216.92.131.65 Probable Submitter: 194.121.118.146 ---- Manual Page -- http://www.php.net/manual/en/function.wordwrap.php Edit -- https://master.php.net/note/edit/83488 Del: integrated -- https://master.php.net/note/delete/83488/integrated Del: useless -- https://master.php.net/note/delete/83488/useless Del: bad code -- https://master.php.net/note/delete/83488/bad+code Del: spam -- https://master.php.net/note/delete/83488/spam Del: non-english -- https://master.php.net/note/delete/83488/non-english Del: in docs -- https://master.php.net/note/delete/83488/in+docs Del: other reasons-- https://master.php.net/note/delete/83488 Reject -- https://master.php.net/note/reject/83488 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#139912) next »