| Conditions | 5 |
| Paths | 1 |
| Total Lines | 25 |
| Code Lines | 18 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 14 | public static function wordwrap(string $string, int $width, string $break = "\n") : string |
||
| 15 | { |
||
| 16 | return implode( |
||
| 17 | $break, |
||
| 18 | array_map(function (string $line) use ($width, $break) { |
||
| 19 | $line = rtrim($line); |
||
| 20 | if (mb_strlen($line) <= $width) { |
||
| 21 | return $line; |
||
| 22 | } |
||
| 23 | |||
| 24 | $words = explode(' ', $line); |
||
| 25 | $line = ''; |
||
| 26 | $actual = ''; |
||
| 27 | foreach ($words as $word) { |
||
| 28 | if (mb_strlen($actual . $word) <= $width) { |
||
| 29 | $actual .= $word . ' '; |
||
| 30 | } else { |
||
| 31 | if ($actual !== '') { |
||
| 32 | $line .= rtrim($actual) . $break; |
||
| 33 | } |
||
| 34 | $actual = $word . ' '; |
||
| 35 | } |
||
| 36 | } |
||
| 37 | return $line . trim($actual); |
||
| 38 | }, explode("\n", $string)) |
||
| 39 | ); |
||
| 52 |