From 456acec4ccab176ac44004aedd2efa72a736f8bb Mon Sep 17 00:00:00 2001 From: Tony Butler Date: Tue, 25 Feb 2014 17:11:30 +0000 Subject: [PATCH] MDL-44349 core_moodlelib: Improve word counting rules Signed-off-by: Tony Butler --- lib/moodlelib.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 968934ab497..8fd8e7db66f 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -7700,7 +7700,16 @@ function moodle_setlocale($locale='') { */ function count_words($string) { $string = strip_tags($string); - return count(preg_split("/\w\b/", $string)) - 1; + // 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; } /**