Commit dd2c05b1 authored by Steven Wittens's avatar Steven Wittens

Truncate UTF-8 patch. Introduced a new function truncate_utf8() for chopping...

Truncate UTF-8 patch. Introduced a new function truncate_utf8() for chopping off strings at unsure locations, without risking incomplete UTF-8 data.
parent e3873fb5
......@@ -903,7 +903,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;
......@@ -1219,6 +1219,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";
......
......@@ -397,7 +397,7 @@ function aggregator_parse_feed(&$data, $feed) {
$title = $item["TITLE"];
}
else {
$title = preg_replace('/^(.*)[^\w;&].*?$/', "\\1", substr($item["DESCRIPTION"], 0, 40));
$title = preg_replace('/^(.*)[^\w;&].*?$/', "\\1", truncate_utf8($item["DESCRIPTION"], 40));
}
/*
......
......@@ -283,7 +283,7 @@ function comment_post($edit) {
$edit["subject"] = strip_tags($edit["subject"]);
if ($edit["subject"] == "") {
$edit["subject"] = substr(strip_tags($edit["comment"]), 0, 29);
$edit["subject"] = truncate_utf8(strip_tags($edit["comment"]), 29);
}
/*
......
......@@ -213,7 +213,7 @@ function node_teaser($body) {
** Nevermind, we split it the hard way ...
*/
return substr($body, 0, $size);
return truncate_utf8($body, $size);
}
......
......@@ -278,7 +278,7 @@ function statistics_admin_accesslog_table($type, $id) {
$user = user_load(array("uid" => $log->uid));
if ($log->url) {
$url = "<a href=\"$log->url\" title=\"$log->url\">". (strlen($log->url) > 28 ? substr($log->url, 0, 28) . '...' : $log->url) ."</a>";
$url = "<a href=\"$log->url\" title=\"$log->url\">". (strlen($log->url) > 28 ? truncate_utf8($log->url, 28) . '...' : $log->url) ."</a>";
}
else {
$url = message_na();
......@@ -326,7 +326,7 @@ function statistics_top_refer() {
$result = pager_query($query, 50, 0, $query_cnt);
while ($referrer = db_fetch_array($result)) {
$rows[] = array("<a href=\"". $referrer["url"] ."\">". substr($referrer["url"], 0, 100) ."</a>", format_date($referrer["last_view"], "small"), $referrer["count"]);
$rows[] = array("<a href=\"". $referrer["url"] ."\">". truncate_utf8($referrer["url"], 100) ."</a>", format_date($referrer["last_view"], "small"), $referrer["count"]);
}
if ($pager = theme("pager", NULL, 50, 0, tablesort_pager())) {
$rows[] = array(array("data" => $pager, "colspan" => 3));
......
......@@ -94,7 +94,7 @@ function watchdog_overview($type) {
while ($watchdog = db_fetch_object($result)) {
$rows[] = array(
array("data" => format_date($watchdog->timestamp, "small"), "class" => "watchdog-$watchdog->type"),
array("data" => substr(strip_tags($watchdog->message), 0, 64), "class" => "watchdog-$watchdog->type"),
array("data" => truncate_utf8(strip_tags($watchdog->message), 64), "class" => "watchdog-$watchdog->type"),
array("data" => format_name($watchdog), "class" => "watchdog-$watchdog->type"),
array("data" => $watchdog->link, "class" => "watchdog-$watchdog->type"),
array("data" => l(t("view details"), "admin/watchdog/view/$watchdog->wid"), "class" => "watchdog-$watchdog->type")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment