public function Visualize($table_data, $font_size) { $ww_table = $this->wordWrapTable($table_data, $font_size, true); $rows = count($ww_table); $cols = array_keys($ww_table[0]); $col_size = array(); if ($this->encoding->useMB()) { $plus = mb_convert_encoding("+", $this->encoding->getEncodingType()); $newline = mb_convert_encoding("\n", $this->encoding->getEncodingType()); $dash = mb_convert_encoding("-", $this->encoding->getEncodingType()); $space = mb_convert_encoding(" ", $this->encoding->getEncodingType()); $vert = mb_convert_encoding('|', $this->encoding->getEncodingType()); $out = mb_convert_encoding("", $this->encoding->getEncodingType()); } else { $plus = "+"; $newline = "\n"; $dash = "-"; $space = " "; $vert = '|'; $out = ""; } $lengths = array(); for ($r = 0; $r < $rows; $r++) { $lengths[$r] = array(); } foreach ($cols as $col) { $max = 0; for ($r = 0; $r < $rows; $r++) { $z = count($ww_table[$r][$col]); for ($i = 0; $i < $z; $i++) { $line = $ww_table[$r][$col][$i]; if ($this->encoding->useMB()) { $w = mb_strlen($line, $this->encoding->getEncodingType()); } else { $w = strlen($line); } $lengths[$r][$col][$i] = $w; if ($w > $max) { $max = $w; } } } $col_size[$col] = $max; } //now do the display for ($r = 0; $r < $rows; $r++) { foreach ($cols as $col) { $out .= $plus; for ($j = 0; $j < $col_size[$col]; $j++) { $out .= $dash; } } $out .= $plus . $newline; $z = count($ww_table[$r][$cols[0]]); for ($i = 0; $i < $z; $i++) { foreach ($cols as $col) { $out .= $vert . $ww_table[$r][$col][$i]; for ($j = $lengths[$r][$col][$i]; $j < $col_size[$col]; $j++) { $out .= $space; } } $out .= $vert . $newline; } } foreach ($cols as $col) { $out .= $plus; for ($j = 0; $j < $col_size[$col]; $j++) { $out .= $dash; } } $out .= $plus . $newline; return $out; }
/** * Get the parts of a word which breaks along hyphenation points or any non-letter. * @param string $word the word we wish to break up * @param bool $supress true (default)to suppress hyphenation points at the beginning/end of a word. * @returns an the associative array has * a string 'Subword' which tells what the subword is, the int 'Offset' tells where the subword started, * the int 'Length' the length of the subword, and the boolean 'IsLetter' which tells us if the * subword is a composed of letters (by the Unicode convention) or not. */ public function getWordParts($word, $supress = true) { $word_parts = array(); $mb_encoding = $this->enc->getEncodingType(); $use_mb = $this->enc->useMB(); mb_regex_encoding($mb_encoding); if ($use_mb) { $word_len = mb_strlen($word, $mb_encoding); } else { $word_len = strlen($word); } if ($mb_encoding != 'UTF-8') { if ($mb_encoding) { $utf8_word = mb_convert_encoding($word, 'UTF-8', $mb_encoding); } else { $utf8_word = mb_convert_encoding($word, 'UTF-8'); } } else { $utf8_word = $word; } $i = 0; $prev_i = 0; $c = ''; do { $sub_word_len = 0; $is_letter = true; while ($i < $word_len && $is_letter) { $c = mb_substr($utf8_word, 0, 1, 'UTF-8'); //get the first character $utf8_word = mb_substr($utf8_word, 1); //delete the first character if (!preg_match('/^\\p{L}/', $c)) { //the character is not a letter $is_letter = false; } $sub_word_len++; $i++; } if (!$is_letter) { //the last character we read was a non-letter $sub_word_len--; } if ($use_mb) { $subword = mb_substr($word, $prev_i, $sub_word_len, $mb_encoding); } else { $subword = substr($word, $prev_i, $sub_word_len); } if ($sub_word_len > 0) { //get the hyphenation points for the word. $hp = $this->HyphenateWord($subword, $mb_encoding, $supress); $num_hp = count($hp); if ($use_mb) { for ($k = 0; $k < $num_hp - 1; $k++) { $word_parts[] = array('Offset' => $prev_i + $hp[$k], 'Length' => $hp[$k + 1] - $hp[$k], 'Subword' => mb_substr($subword, $hp[$k], $hp[$k + 1] - $hp[$k], $mb_encoding), 'IsLetter' => True); } $word_parts[] = array('Offset' => $prev_i + $hp[$num_hp - 1], 'Length' => $sub_word_len - $hp[$num_hp - 1], 'Subword' => mb_substr($subword, $hp[$num_hp - 1], $sub_word_len - $hp[$num_hp - 1], $mb_encoding), 'IsLetter' => True); } else { for ($k = 0; $k < $num_hp - 1; $k++) { $word_parts[] = array('Offset' => $prev_i + $hp[$k], 'Length' => $hp[$k + 1] - $hp[$k], 'Subword' => substr($subword, $hp[$k], $hp[$k + 1] - $hp[$k]), 'IsLetter' => True); } $word_parts[] = array('Offset' => $i + $hp[$num_hp - 1], 'Length' => $sub_word_len - $hp[$num_hp - 1], 'Subword' => substr($subword, $hp[$num_hp - 1]), 'IsLetter' => True); } } if (!$is_letter) { $word_parts[] = array('Offset' => $i, 'Length' => 1, 'Subword' => $c, 'IsLetter' => False); } $prev_i = $i; } while ($i < $word_len); return $word_parts; }