diff options
Diffstat (limited to 'application/bookmark/LinkUtils.php')
-rw-r--r-- | application/bookmark/LinkUtils.php | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/application/bookmark/LinkUtils.php b/application/bookmark/LinkUtils.php index a74fda57..cf97e3b0 100644 --- a/application/bookmark/LinkUtils.php +++ b/application/bookmark/LinkUtils.php | |||
@@ -176,3 +176,49 @@ function is_note($linkUrl) | |||
176 | { | 176 | { |
177 | return isset($linkUrl[0]) && $linkUrl[0] === '?'; | 177 | return isset($linkUrl[0]) && $linkUrl[0] === '?'; |
178 | } | 178 | } |
179 | |||
180 | /** | ||
181 | * Extract an array of tags from a given tag string, with provided separator. | ||
182 | * | ||
183 | * @param string|null $tags String containing a list of tags separated by $separator. | ||
184 | * @param string $separator Shaarli's default: ' ' (whitespace) | ||
185 | * | ||
186 | * @return array List of tags | ||
187 | */ | ||
188 | function tags_str2array(?string $tags, string $separator): array | ||
189 | { | ||
190 | // For whitespaces, we use the special \s regex character | ||
191 | $separator = $separator === ' ' ? '\s' : $separator; | ||
192 | |||
193 | return preg_split('/\s*' . $separator . '+\s*/', trim($tags) ?? '', -1, PREG_SPLIT_NO_EMPTY); | ||
194 | } | ||
195 | |||
196 | /** | ||
197 | * Return a tag string with provided separator from a list of tags. | ||
198 | * Note that given array is clean up by tags_filter(). | ||
199 | * | ||
200 | * @param array|null $tags List of tags | ||
201 | * @param string $separator | ||
202 | * | ||
203 | * @return string | ||
204 | */ | ||
205 | function tags_array2str(?array $tags, string $separator): string | ||
206 | { | ||
207 | return implode($separator, tags_filter($tags, $separator)); | ||
208 | } | ||
209 | |||
210 | /** | ||
211 | * Clean an array of tags: trim + remove empty entries | ||
212 | * | ||
213 | * @param array|null $tags List of tags | ||
214 | * @param string $separator | ||
215 | * | ||
216 | * @return array | ||
217 | */ | ||
218 | function tags_filter(?array $tags, string $separator): array | ||
219 | { | ||
220 | $trimDefault = " \t\n\r\0\x0B"; | ||
221 | return array_values(array_filter(array_map(function (string $entry) use ($separator, $trimDefault): string { | ||
222 | return trim($entry, $trimDefault . $separator); | ||
223 | }, $tags ?? []))); | ||
224 | } | ||