Permalink
Browse files

multibyte word wrap

  • Loading branch information...
henrik242 authored and Henrik Brautaset Aronsen committed May 5, 2009
1 parent c430710 commit 8a4225e19cd0735861060750cb19507fec3080ea
Showing with 18 additions and 5 deletions.
  1. +16 −3 src/musicbrowser.php
  2. +2 −2 test/UtilTest.php
View
@@ -128,6 +128,9 @@ function mb_substr($str, $start, $length = 1024, $encoding = false) {
function mb_convert_case($str, $mode, $encoding = false) {
return ucwords($str);
}
+ function mb_strlen($str, $encoding = false) {
+ return strlen($str);
+ }
}
if (!function_exists('utf8_encode')) {
$message .= "Warning: Your PHP installation lacks the XML Parser Functions extension<br>";
@@ -1046,7 +1049,7 @@ function js_url_path() {
}
function display_item() {
- $displayItem = Util::word_wrap($this->item);
+ $displayItem = Util::word_wrap($this->item, $this->charset);
$displayItem = Util::convert_to_utf8($displayItem, $this->charset);
return $displayItem;
}
@@ -1140,8 +1143,18 @@ function strip($str) {
return preg_replace('/[^\x20-\x21\x23-\x5b\x5d-\xff]/', "", $str);
}
- function word_wrap($item) {
- return wordwrap(preg_replace("/_/", " ", $item), 40, " ", true);
+ function word_wrap($item, $charset) {
+ $pieces = split(" ", preg_replace("/_/", " ", $item));
+ $result = array();
+ foreach ($pieces as $piece) {
+ $current = $piece;
+ while (mb_strlen($current) > 40) {
+ $result[] = mb_substr($current, 0, 40, $charset);
+ $current = mb_substr($current, 40, 2048, $charset);
+ }
+ $result[] = $current;
+ }
+ return implode(" ", $result);
}
/**
View
@@ -40,8 +40,8 @@ public function testStrip() {
}
public function testWordWrap() {
- $this->assertEquals("abcdefghijklmnopqrstuvwxyzæøåabcdefgh ijklmnopqrstuvwxyzæøåabcdefghijklmnop qrstuvwxyz",
- $this->util->word_wrap("abcdefghijklmnopqrstuvwxyzæøåabcdefghijklmnopqrstuvwxyzæøåabcdefghijklmnopqrstuvwxyz"));
+ $this->assertEquals("abcdefghijklmnopqrstuvwxyzæøåabcdefghiæø åæøåæøåjklmnopqrstuvwxyzæøåabcdefghijklm nopqrstuvwxyz",
+ $this->util->word_wrap("abcdefghijklmnopqrstuvwxyzæøåabcdefghiæøåæøåæøåjklmnopqrstuvwxyzæøåabcdefghijklmnopqrstuvwxyz", "utf-8"));
}
public function testConvertToUtf8() {

0 comments on commit 8a4225e

Please sign in to comment.