Skip to content

Commit b0c9925

Browse files
committed
Improved wordwrap function
1 parent aa0dda7 commit b0c9925

1 file changed

Lines changed: 24 additions & 20 deletions

File tree

src/php-utf-8/functions/wordwrap.php

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,58 +18,62 @@
1818
* @package php-utf-8
1919
* @subpackage functions
2020
*/
21+
/* Function from https://github.com/nicolas-grekas/Patchwork-UTF8 */
2122
function wordwrap ($str, $width = 75, $break = "\n", $cut = FALSE)
2223
{
23-
$width = (int)$width;
24+
$width = (int)$width;
2425
$str = explode($break, $str);
2526

26-
$iLen = count($str);
27-
$result = array();
28-
$line = '';
29-
$lineLen = 0;
27+
$i_len = count($str);
28+
$result = array();
29+
$line = '';
30+
$line_len = 0;
3031

31-
for ($i = 0; $i < $iLen; ++$i)
32+
for ($i = 0; $i < $i_len; ++$i)
3233
{
33-
$words = explode(' ', $str[$i]);
34+
$words = explode(' ', $str[$i]);
3435
$line && $result[] = $line;
35-
$lineLen = len($line);
36-
$jLen = count($words);
36+
$line_len = len($line);
37+
$jLen = count($words);
3738

3839
for ($j = 0; $j < $jLen; ++$j)
3940
{
4041
$w = $words[$j];
4142
$wLen = len($w);
4243

43-
if ($lineLen + $wLen < $width)
44+
if ($line_len + $wLen < $width)
4445
{
4546
if ($j)
47+
{
4648
$line .= ' ';
47-
$line .= $w;
48-
$lineLen += $wLen + 1;
49+
}
50+
$line .= $w;
51+
$line_len += $wLen + 1;
4952
}
5053
else
5154
{
5255
if ($j || $i)
56+
{
5357
$result[] = $line;
54-
$line = '';
55-
$lineLen = 0;
58+
}
59+
$line = '';
60+
$line_len = 0;
5661

5762
if ($cut && $wLen > $width)
5863
{
5964
$w = split($w);
6065

6166
do
6267
{
63-
$result[] = implode('', array_slice($w, 0, $width));
64-
$line = implode('', $w = array_slice($w, $width));
65-
$lineLen = $wLen -= $width;
68+
$result[] = implode('', array_slice($w, 0, $width));
69+
$line = implode('', $w = array_slice($w, $width));
70+
$line_len = $wLen -= $width;
6671
}
6772
while ($wLen > $width);
6873
$w = implode('', $w);
6974
}
70-
71-
$line = $w;
72-
$lineLen = $wLen;
75+
$line = $w;
76+
$line_len = $wLen;
7377
}
7478
}
7579
}

0 commit comments

Comments
 (0)