@@ -110,6 +110,46 @@ function getRequestedTheme(array $params): array
110
110
return $ theme ;
111
111
}
112
112
113
+ /**
114
+ * Wraps a string to a given number of characters
115
+ *
116
+ * Similar to `wordwrap()`, but uses regex and does not break with certain non-ascii characters
117
+ *
118
+ * @param string $string The input string
119
+ * @param int $width The number of characters at which the string will be wrapped
120
+ * @param string $break The line is broken using the optional `break` parameter
121
+ * @param bool $cut_long_words If the `cut_long_words` parameter is set to true, the string is
122
+ * the string is always wrapped at or before the specified width. So if you have
123
+ * a word that is larger than the given width, it is broken apart.
124
+ * When false the function does not split the word even if the width is smaller
125
+ * than the word width.
126
+ * @return string The given string wrapped at the specified length
127
+ */
128
+ function utf8WordWrap ($ string , $ width = 75 , $ break = "\n" , $ cut_long_words = false )
129
+ {
130
+ // match anything 1 to $width chars long followed by whitespace or EOS
131
+ $ string = preg_replace ("/(.{1, $ width})(?:\s|$)/uS " , "$1 $ break " , $ string );
132
+ // split words that are too long after being broken up
133
+ if ($ cut_long_words ) {
134
+ $ string = preg_replace ("/(\S{ " . $ width . "})(?=\S)/u " , "$1 $ break " , $ string );
135
+ }
136
+ // trim any trailing line breaks
137
+ return rtrim ($ string , $ break );
138
+ }
139
+
140
+ /**
141
+ * Get the length of a string with utf8 characters
142
+ *
143
+ * Similar to `strlen()`, but uses regex and does not break with certain non-ascii characters
144
+ *
145
+ * @param string $string The input string
146
+ * @return int The length of the string
147
+ */
148
+ function utf8Strlen ($ string )
149
+ {
150
+ return preg_match_all ("/./us " , $ string , $ matches );
151
+ }
152
+
113
153
/**
114
154
* Split lines of text using <tspan> elements if it contains a newline or exceeds a maximum number of characters
115
155
*
@@ -122,18 +162,19 @@ function getRequestedTheme(array $params): array
122
162
function splitLines (string $ text , int $ maxChars , int $ line1Offset ): string
123
163
{
124
164
// if too many characters, insert \n before a " " or "-" if possible
125
- if (mb_strlen ($ text ) > $ maxChars && strpos ($ text , "\n" ) === false ) {
165
+ if (utf8Strlen ($ text ) > $ maxChars && strpos ($ text , "\n" ) === false ) {
126
166
// prefer splitting at " - " if possible
127
167
if (strpos ($ text , " - " ) !== false ) {
128
168
$ text = str_replace (" - " , "\n- " , $ text );
129
169
}
130
- // otherwise, use word wrap to split at " "
170
+ // otherwise, use word wrap to split at spaces
131
171
else {
132
- $ text = wordwrap ($ text , $ maxChars , "\n" , true );
172
+ $ text = utf8WordWrap ($ text , $ maxChars , "\n" , true );
133
173
}
134
174
}
175
+ $ text = htmlspecialchars ($ text );
135
176
return preg_replace (
136
- "/^(.*) \n(.*)$ / " ,
177
+ "/^(.*) \n(.*)/ " ,
137
178
"<tspan x='81.5' dy=' {$ line1Offset }'>$1</tspan><tspan x='81.5' dy='16'>$2</tspan> " ,
138
179
$ text
139
180
);
0 commit comments