@@ -75,34 +75,42 @@ protected function printTextBlock(\DOMNode $node)
75
75
$ text = str_replace ('<em> ' , '* ' , $ text );
76
76
$ text = str_replace ('</em> ' , '* ' , $ text );
77
77
78
- $ lines = [];
79
- $ tempLine = '' ;
80
- $ words = explode (' ' , $ text );
78
+ $ nodeLines = explode ("\n" , $ text );
79
+ $ lines = [];
80
+
81
+ foreach ($ nodeLines as $ currentLine ) {
82
+ $ currentLine = trim ($ currentLine );
83
+ if ($ currentLine === '' ) {
84
+ // The text contained a blank line. Respect this.
85
+ $ lines [] = '' ;
86
+ continue ;
87
+ }
81
88
82
- foreach ($ words as $ word ) {
83
- if (strlen ($ tempLine .$ word ) >= 99 ) {
84
- if (strlen ($ tempLine .$ word ) === 99 ) {
85
- // Adding the extra space will push us to the edge
86
- // so we are done.
87
- $ lines [] = $ tempLine .$ word ;
88
- $ tempLine = '' ;
89
- } else if (strlen ($ tempLine .$ word ) === 100 ) {
89
+ $ tempLine = '' ;
90
+ $ words = explode (' ' , $ currentLine );
91
+
92
+ foreach ($ words as $ word ) {
93
+ $ currentLength = strlen ($ tempLine .$ word );
94
+ if ($ currentLength < 99 ) {
95
+ $ tempLine .= $ word .' ' ;
96
+ continue ;
97
+ }
98
+
99
+ if ($ currentLength === 99 || $ currentLength === 100 ) {
90
100
// We are already at the edge, so we are done.
91
101
$ lines [] = $ tempLine .$ word ;
92
102
$ tempLine = '' ;
93
103
} else {
94
104
$ lines [] = rtrim ($ tempLine );
95
105
$ tempLine = $ word .' ' ;
96
106
}
97
- } else {
98
- $ tempLine .= $ word .' ' ;
107
+ }//end foreach
108
+
109
+ if ($ tempLine !== '' ) {
110
+ $ lines [] = rtrim ($ tempLine );
99
111
}
100
112
}//end foreach
101
113
102
- if ($ tempLine !== '' ) {
103
- $ lines [] = rtrim ($ tempLine );
104
- }
105
-
106
114
echo implode (PHP_EOL , $ lines ).PHP_EOL .PHP_EOL ;
107
115
108
116
}//end printTextBlock()
0 commit comments