From b312d2f18e24ad980e647884e1bae6c26ceb82d8 Mon Sep 17 00:00:00 2001 From: Mohammad Javad Naderi Date: Thu, 22 Aug 2013 13:23:15 +0430 Subject: character_limiter now works correct for UTF-8 strings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit strlen() doesn't return the actual length for unicode strings. For example strlen('سلام') returns 8, but length of سلام is 4. strlen(utf8_decode('سلام')) returns correct value 4. Reference: http://www.php.net/manual/de/function.strlen.php#45407 --- system/helpers/text_helper.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index b2351db95..4ddf648cb 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -85,7 +85,7 @@ if ( ! function_exists('character_limiter')) */ function character_limiter($str, $n = 500, $end_char = '…') { - if (strlen($str) < $n) + if (strlen(utf8_decode($str)) < $n) { return $str; } @@ -93,7 +93,7 @@ if ( ! function_exists('character_limiter')) // a bit complicated, but faster than preg_replace with \s+ $str = preg_replace('/ {2,}/', ' ', str_replace(array("\r", "\n", "\t", "\x0B", "\x0C"), ' ', $str)); - if (strlen($str) <= $n) + if (strlen(utf8_decode($str)) <= $n) { return $str; } @@ -103,10 +103,10 @@ if ( ! function_exists('character_limiter')) { $out .= $val.' '; - if (strlen($out) >= $n) + if (strlen(utf8_decode($out)) >= $n) { $out = trim($out); - return (strlen($out) === strlen($str)) ? $out : $out.$end_char; + return (strlen(utf8_decode($out)) === strlen(utf8_decode($str))) ? $out : $out.$end_char; } } } @@ -537,4 +537,4 @@ if ( ! function_exists('ellipsize')) } /* End of file text_helper.php */ -/* Location: ./system/helpers/text_helper.php */ \ No newline at end of file +/* Location: ./system/helpers/text_helper.php */ -- cgit v1.2.3-24-g4f1b From 9aebeaf6efa69a63c1518f1c7b0886e510909d29 Mon Sep 17 00:00:00 2001 From: Mohammad Javad Naderi Date: Thu, 22 Aug 2013 16:25:55 +0430 Subject: remove newline from end of file --- system/helpers/text_helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index 4ddf648cb..8dae3be4e 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -537,4 +537,4 @@ if ( ! function_exists('ellipsize')) } /* End of file text_helper.php */ -/* Location: ./system/helpers/text_helper.php */ +/* Location: ./system/helpers/text_helper.php */ \ No newline at end of file -- cgit v1.2.3-24-g4f1b From c6f5ed16c6a4a146deb40870781442d0936ac33a Mon Sep 17 00:00:00 2001 From: Mohammad Javad Naderi Date: Thu, 22 Aug 2013 18:33:50 +0430 Subject: count number of characters (with mb_strlen) instead of counting number of bytes with strlen --- system/helpers/text_helper.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index 8dae3be4e..bfbdc2f00 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -85,7 +85,7 @@ if ( ! function_exists('character_limiter')) */ function character_limiter($str, $n = 500, $end_char = '…') { - if (strlen(utf8_decode($str)) < $n) + if (mb_strlen($str) < $n) { return $str; } @@ -93,7 +93,7 @@ if ( ! function_exists('character_limiter')) // a bit complicated, but faster than preg_replace with \s+ $str = preg_replace('/ {2,}/', ' ', str_replace(array("\r", "\n", "\t", "\x0B", "\x0C"), ' ', $str)); - if (strlen(utf8_decode($str)) <= $n) + if (mb_strlen($str) <= $n) { return $str; } @@ -103,10 +103,10 @@ if ( ! function_exists('character_limiter')) { $out .= $val.' '; - if (strlen(utf8_decode($out)) >= $n) + if (mb_strlen($out) >= $n) { $out = trim($out); - return (strlen(utf8_decode($out)) === strlen(utf8_decode($str))) ? $out : $out.$end_char; + return (mb_strlen($out) === mb_strlen($str)) ? $out : $out.$end_char; } } } -- cgit v1.2.3-24-g4f1b From 871754af60251993d640981e107d2def5f2db396 Mon Sep 17 00:00:00 2001 From: darwinel Date: Tue, 11 Feb 2014 17:34:57 +0100 Subject: 2013 > 2014 Update copyright notices from 2013 to 2014. And update one calendar example in user_guide from year 2013/2014 to 2014/2015. --- system/helpers/text_helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index bda844630..206ce5559 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -18,7 +18,7 @@ * * @package CodeIgniter * @author EllisLab Dev Team - * @copyright Copyright (c) 2008 - 2013, EllisLab, Inc. (http://ellislab.com/) + * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) * @link http://codeigniter.com * @since Version 1.0 -- cgit v1.2.3-24-g4f1b From 6ce4746474ddf050f7f4df61b7a22b7f5854d533 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Thu, 13 Feb 2014 03:28:00 +0200 Subject: Update Text and Inflector helpers to utilize mbstring, if available Text: word_wrap(), ellipsize() Inflector: humanize(), underscore() (rel #2855) --- system/helpers/text_helper.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index 45e8ae760..76e1735b5 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -445,14 +445,14 @@ if ( ! function_exists('word_wrap')) { // Is the line within the allowed character count? // If so we'll join it to the output and continue - if (strlen($line) <= $charlim) + if (mb_strlen($line) <= $charlim) { $output .= $line."\n"; continue; } $temp = ''; - while ((strlen($line)) > $charlim) + while (mb_strlen($line) > $charlim) { // If the over-length word is a URL we won't wrap it if (preg_match('!\[url.+\]|://|wwww.!', $line)) @@ -461,8 +461,8 @@ if ( ! function_exists('word_wrap')) } // Trim the word down - $temp .= substr($line, 0, $charlim - 1); - $line = substr($line, $charlim - 1); + $temp .= mb_substr($line, 0, $charlim - 1); + $line = mb_substr($line, $charlim - 1); } // If $temp contains data it means we had to split up an over-length @@ -512,21 +512,21 @@ if ( ! function_exists('ellipsize')) $str = trim(strip_tags($str)); // Is the string long enough to ellipsize? - if (strlen($str) <= $max_length) + if (mb_strlen($str) <= $max_length) { return $str; } - $beg = substr($str, 0, floor($max_length * $position)); + $beg = mb_substr($str, 0, floor($max_length * $position)); $position = ($position > 1) ? 1 : $position; if ($position === 1) { - $end = substr($str, 0, -($max_length - strlen($beg))); + $end = mb_substr($str, 0, -($max_length - mb_strlen($beg))); } else { - $end = substr($str, -($max_length - strlen($beg))); + $end = mb_substr($str, -($max_length - mb_strlen($beg))); } return $beg.$ellipsis.$end; -- cgit v1.2.3-24-g4f1b