-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordwrap.php
35 lines (24 loc) · 840 Bytes
/
wordwrap.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php
$length = $argv[1];
$break = $argv[2];
$input = $argv[3];
echo "Here is the original:\n\n" . $input . "\n";
$wrapped = '';
$whitespace = " ";
$punctuation = ".";
while (strlen($input) > $length) {
// echo "input: " . $input . "\n";
$segment = substr($input, 0, $length) . "\n";
// echo "Segment: " . $segment . "\n";
$lastBreakPosition = $length;
if ((strpos($segment, $whitespace)) && $segment[$length] != $whitespace) {
$lastBreakPosition = strrpos($segment, $whitespace);
// echo "Last break position: " . $lastBreakPosition . "\n";
$segment = substr($input, 0, $lastBreakPosition);
}
$wrapped .= trim($segment) . $break . "\n";
$input = substr($input, $lastBreakPosition);
}
$wrapped .= trim($input);
echo "\n\nHere is the wrapped version:\n\n" . $wrapped . "\n";
?>