MDL-70796 count_words: match the count from LibraOffice & MS Word

This commit is contained in:
Tim Hunt
2021-02-05 15:28:34 +00:00
parent 726d5dfc65
commit cfcfaba862
2 changed files with 26 additions and 16 deletions
+7 -7
View File
@@ -8401,14 +8401,14 @@ function count_words($string) {
$string = strip_tags($string);
// Decode HTML entities.
$string = html_entity_decode($string);
// Replace underscores (which are classed as word characters) with spaces.
$string = preg_replace('/_/u', ' ', $string);
// Remove any characters that shouldn't be treated as word boundaries.
$string = preg_replace('/[\'"-]/u', '', $string);
// Remove dots and commas from within numbers only.
$string = preg_replace('/([0-9])[.,]([0-9])/u', '$1$2', $string);
return count(preg_split('/\w\b/u', $string)) - 1;
// Now, the word count is the number of blocks of characters separated
// by any sort of space. That seems to be the definition used by all other systems.
// To be precise about what is considered to separate words:
// * Anything that Unicode considers a 'Separator'
// * Anything that Unicode considers a 'Control character'
// * An em- or en- dash.
return count(preg_split('~[\p{Z}\p{Cc}—–]+~u', $string, -1, PREG_SPLIT_NO_EMPTY));
}
/**