note 106088 added to function.wordwrap

From: Date: Sun, 09 Oct 2011 00:35:13 +0000
Subject: note 106088 added to function.wordwrap
Groups: php.notes 
Request: Send a blank email to php-notes+get-183731@lists.php.net to get a copy of this message
A completly working, multibyte-safe, version of wordwrap (as I contributed it to the Zend Framework in Zend_Text_MultiByte). It's mostly a port of the original PHP wordwrap function, backed by a lot of unit tests: <?php /** * Word wrap * * @param string $string * @param integer $width * @param string $break * @param boolean $cut * @param string $charset * @return string */ function iconv_wordwrap($string, $width = 75, $break = "\n", $cut = false, $charset = 'utf-8') { $stringWidth = iconv_strlen($string, $charset); $breakWidth = iconv_strlen($break, $charset); if (strlen($string) === 0) { return ''; } elseif ($breakWidth === null) { throw new Zend_Text_Exception('Break string cannot be empty'); } elseif ($width === 0 && $cut) { throw new Zend_Text_Exception('Can\'t force cut when width is zero'); } $result = ''; $lastStart = $lastSpace = 0; for ($current = 0; $current < $stringWidth; $current++) { $char = iconv_substr($string, $current, 1, $charset); if ($breakWidth === 1) { $possibleBreak = $char; } else { $possibleBreak = iconv_substr($string, $current, $breakWidth, $charset); } if ($possibleBreak === $break) { $result .= iconv_substr($string, $lastStart, $current - $lastStart + $breakWidth, $charset); $current += $breakWidth - 1; $lastStart = $lastSpace = $current + 1; } elseif ($char === ' ') { if ($current - $lastStart >= $width) { $result .= iconv_substr($string, $lastStart, $current - $lastStart, $charset) . $break; $lastStart = $current + 1; } $lastSpace = $current; } elseif ($current - $lastStart >= $width && $cut && $lastStart >= $lastSpace) { $result .= iconv_substr($string, $lastStart, $current - $lastStart, $charset) . $break; $lastStart = $lastSpace = $current; } elseif ($current - $lastStart >= $width && $lastStart < $lastSpace) { $result .= iconv_substr($string, $lastStart, $lastSpace - $lastStart, $charset) . $break; $lastStart = $lastSpace = $lastSpace + 1; } } if ($lastStart !== $current) { $result .= iconv_substr($string, $lastStart, $current - $lastStart, $charset); } return $result; } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 85.216.120.3 ---- Manual Page -- http://www.php.net/manual/en/function.wordwrap.php Edit -- https://master.php.net/note/edit/106088 Del: integrated -- https://master.php.net/note/delete/106088/integrated Del: useless -- https://master.php.net/note/delete/106088/useless Del: bad code -- https://master.php.net/note/delete/106088/bad+code Del: spam -- https://master.php.net/note/delete/106088/spam Del: non-english -- https://master.php.net/note/delete/106088/non-english Del: in docs -- https://master.php.net/note/delete/106088/in+docs Del: other reasons-- https://master.php.net/note/delete/106088 Reject -- https://master.php.net/note/reject/106088 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#183731) next »