summaryrefslogtreecommitdiff
path: root/includes
diff options
context:
space:
mode:
Diffstat (limited to 'includes')
-rw-r--r--includes/common.inc27
1 files changed, 26 insertions, 1 deletions
diff --git a/includes/common.inc b/includes/common.inc
index 93d964eed..4433791b8 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -916,7 +916,7 @@ function format_name($object) {
*/
if (strlen($object->name) > 20) {
- $name = substr($object->name, 0, 15) ."...";
+ $name = truncate_utf8($object->name, 15) ."...";
}
else {
$name = $object->name;
@@ -1234,6 +1234,31 @@ function drupal_xml_parser_create(&$data) {
return $xml_parser;
}
+/**
+ * UTF-8-safe string truncation
+ * If the end position is in the middle of a UTF-8 sequence, it scans backwards
+ * until the beginning of the byte sequence.
+ *
+ * Use this function whenever you want to chop off a string at an unsure
+ * location. On the other hand, if you're sure that you're splitting on a
+ * character boundary (e.g. after using strpos or similar), you can safely use
+ * substr() instead.
+ *
+ * @param $string The string to truncate
+ * @param $len An upper limit on the returned string length.
+ */
+function truncate_utf8($string, $len) {
+ $slen = strlen($string);
+ if ($slen <= $len) {
+ return $string;
+ }
+ if ((ord($string[$len]) < 0x80) || (ord($string[$len]) >= 0xC0)) {
+ return substr($string, 0, $len);
+ }
+ while (ord($string[--$len]) < 0xC0) {};
+ return substr($string, 0, $len);
+}
+
include_once "includes/theme.inc";
include_once "includes/pager.inc";
include_once "includes/menu.inc";